{"id":6743,"date":"2021-05-12T06:59:44","date_gmt":"2021-05-12T04:59:44","guid":{"rendered":"http:\/\/bkovertretung.bplaced.net\/wordpress\/?page_id=6743"},"modified":"2024-09-13T11:05:19","modified_gmt":"2024-09-13T09:05:19","slug":"vba-syntax-beispiele","status":"publish","type":"page","link":"http:\/\/bkovertretung.bplaced.net\/wordpress\/programmieren-in-vba\/vba-syntax-beispiele\/","title":{"rendered":"VBA-Syntax &#038; Beispiele"},"content":{"rendered":"<p><span style=\"color: #ff0000;\"><em><strong>Achtung &#8220; &#8220; werden hier falsch angezeigt, sie m\u00fcssen immer oben stehen!!!!\u00a0<\/strong><\/em><\/span><\/p>\n<h2>1. Grunds\u00e4tzliches<\/h2>\n<p>In VBA (sowie in vielen anderen Programmiersprachen) wird erst das <em><strong><span style=\"color: #ff00ff;\">Objekt<\/span><\/strong><\/em> (zum Beispiel eine <em><span style=\"color: #ff00ff;\">Zelle<\/span><\/em>) genannt mit dem etwas passieren soll, dann die <em><strong><span style=\"color: #ff00ff;\">Eigenschaft<\/span><\/strong> <\/em><span style=\"color: #000000;\"><span style=\"caret-color: #000000;\">(zum Beispiel <em><span style=\"color: #ff00ff;\">Farbe<\/span><\/em>) und dann die konkrete <em><span style=\"color: #ff00ff;\">Auspr\u00e4gung<\/span><\/em> (zum Beispiel <em><span style=\"color: #ff00ff;\">pink<\/span><\/em>).<\/span><\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"color: #339966;\"><span style=\"color: #000000;\"><strong>Beispiel<\/strong><br \/>\n<\/span>Range(&#8222;E5&#8220;).Font.Color = 1500<br \/>\n<span style=\"color: #000000;\">oder<\/span><br \/>\nCells(6, 5).Value = &#8222;Dies ist eine VBA-\u00dcbung&#8220;<br \/>\n<\/span><\/p>\n<h3>Inhalt von Zellen &amp; Formatierung<\/h3>\n<p>Einzelne Zellen k\u00f6nnen mit den Befehlen<\/p>\n<ul>\n<li><span style=\"color: #008080;\"><em>Range(&#8222;E5&#8220;)<\/em> <\/span>-&gt; Spalte E Zeile 5<br \/>\noder mit<\/li>\n<li><em><span style=\"color: #008080;\">Cells(3,5)<\/span> <\/em>\u00a0-&gt; 3. Zeile, 5. Spalte (E)<br \/>\nangesprochen\u00a0werden.<\/li>\n<\/ul>\n<p>Mit k\u00f6nnen allerdings auch Bereiche festgelegt werden:<\/p>\n<p>Bsp:\u00a0<em><span style=\"color: #008080;\">Range(&#8222;B3:E10&#8220;).value = &#8222;xxx&#8220;\u00a0<\/span><br \/>\nschreibt in jede Zelle (also in 32 Zellen!!) des Bereiches xxx rein.<\/em><\/p>\n<h2><em><strong>Eigenschaften von Zellen<\/strong><\/em><\/h2>\n<p>Grunds\u00e4tzlich k\u00f6nnen dies alle Eigenschaften sein, die Du in Excel festlegen kannst. Dies sind zum Beispiel:<\/p>\n<h4>Schriftfarbe:<em><em><span style=\"color: #008000;\">\u00a0Font.Color \u00a0=\u00a0VbColor Name\u00a0<\/span><\/em><\/em><\/h4>\n<ul>\n<li>\n<pre class=\"wp-block-code language-visual-basic\" tabindex=\"0\"><code class=\"language-visual-basic\">Range<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"a1\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>Font<span class=\"token punctuation\">.<\/span>Color <span class=\"token operator\">=<\/span> vbRed<\/code><\/pre>\n<p><em><span style=\"color: #008000;\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8620 alignright\" src=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/Bildschirmfoto-2023-09-03-um-18.37.48-300x162.png\" alt=\"Bildschirmfoto 2023-09-03 um 18.37.48\" width=\"265\" height=\"143\" srcset=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/Bildschirmfoto-2023-09-03-um-18.37.48-300x162.png 300w, http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/Bildschirmfoto-2023-09-03-um-18.37.48-768x414.png 768w, http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/Bildschirmfoto-2023-09-03-um-18.37.48.png 806w\" sizes=\"auto, (max-width: 265px) 100vw, 265px\" \/><\/span><\/em><\/p>\n<ul>\n<li>vbBlack (schwarz)<\/li>\n<li>vbBlue<\/li>\n<li>vbCyan (t\u00fcrkis)<\/li>\n<li>vbGreen<\/li>\n<li>vbMagenta (pink)<\/li>\n<li>vbRed<\/li>\n<li>Ivbwhite<\/li>\n<li>vbYellow<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>oder:<\/p>\n<p><code class=\"language-visual-basic\" style=\"white-space: pre-wrap;\">Range<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"a1\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>Font<span class=\"token punctuation\">.<\/span>Color <span class=\"token operator\">=<\/span> RGB<span class=\"token punctuation\">(<\/span><span class=\"token number\">255<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">255<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<br \/>\n<\/span><\/code><code class=\"language-visual-basic\" style=\"white-space: pre-wrap;\"><span class=\"token punctuation\">Cells(zeile, spalte).Font.Color = RGB(0,5,100)<\/span><\/code><\/p>\n<p><code class=\"language-visual-basic\"><\/code>RGB steht f\u00fcr rot, gr\u00fcn, blau. Die Zahlen bewegen sich jeweils zwischen 0 und 256. Welche Zahlen eine Farbe ergeben kannst Du hier nachlesen:<br \/>\nh<a href=\"https:\/\/www.farb-tabelle.de\/de\/farbtabelle.htm\" target=\"_blank\">ttps:\/\/www.farb-tabelle.de\/de\/farbtabelle.htm<\/a><\/p>\n<h4><em><span style=\"color: #008000;\">\u00a0<\/span><\/em>Hintergrundfarbe der Zelle <span style=\"color: #008000;\"><em>Interior.ColorIndex.<\/em><\/span><\/h4>\n<ul>\n<li><code class=\"language-visual-basic\" style=\"white-space: pre-wrap;\"><strong><span style=\"color: #ff6600;\">Beispiele<\/span><\/strong>:<br \/>\n<span style=\"color: #ff6600;\">Range<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"a1\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>Font<span class=\"token punctuation\">.<\/span>ColorIndex <span class=\"token operator\">=<\/span> 36<\/span><br \/>\n<\/code><span style=\"white-space: pre-wrap;\"><span style=\"color: #ff6600;\">Cells(zeile, spalte).Interior.ColorIndex = 45<\/span><\/span><\/li>\n<\/ul>\n<p><code class=\"language-visual-basic\"><\/code><span style=\"color: #008000;\"><span style=\"color: #000000;\">Der Colorindex ist eine Zahl zwischen <span style=\"color: #ff00ff;\"><em><strong>1 und 56.<\/strong><\/em><\/span> Welche Hintergrundfarbe sich ergibt kannst Du ausprobieren.<\/span><\/span><\/p>\n<p><a href=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/Hintergrundfarben-\u00dcbersicht.pdf\">Hintergrundfarben-\u00dcbersicht<\/a><\/p>\n<h4>Umrandungen<\/h4>\n<ul>\n<li><em><span style=\"color: #008000;\">.BorderAround<br \/>\n<\/span><\/em><span style=\"color: #000000;\">Details zur Rahmenformatierung findest Du hier:<\/span><em><span style=\"color: #008000;\"><a href=\"https:\/\/www.automateexcel.com\/de\/vba\/zellenrahmen\/\" target=\"_blank\"> https:\/\/www.automateexcel.com\/de\/vba\/zellenrahmen\/<\/a><br \/>\n<\/span><\/em><\/li>\n<li>Bsp.:<br \/>\n<span style=\"color: #008000;\"><em>Range(<span class=\"hljs-string\">&#8222;C4:F15&#8220;<\/span>)<\/em><code class=\"language-visual-basic\" style=\"white-space: pre-wrap;\"><span class=\"token punctuation\">.<\/span>BorderAround LineStyle<span class=\"token punctuation\">:<\/span><span class=\"token operator\">=<\/span>xlContinuous<span class=\"token punctuation\">,<\/span> Weight<span class=\"token punctuation\">:<\/span><span class=\"token operator\">=<\/span>xlThick<span class=\"token punctuation\">,<\/span> Color<span class=\"token punctuation\">:<\/span><span class=\"token operator\">=<\/span>vbRed<\/code><\/span><\/p>\n<aside id=\"right-column\" class=\"sidebar-vba\" data-location=\"Right Column\" data-section-name=\"VBA Sidebar\" data-section-design-id=\"3\">\n<div id=\"automateexcel_sidebar_bottom\" class=\"pg-loaded\"><\/div>\n<\/aside>\n<\/li>\n<\/ul>\n<h4>L\u00f6schen<\/h4>\n<p>Inhalt &amp; Formatierung l\u00f6schen: <span style=\"color: #008080;\"><em>clear<\/em><\/span><\/p>\n<p><span style=\"color: #ff6600;\">Bsp.: Range(Cells(1, 1), Cells(5, 4)).Clear<\/span><br \/>\n<span style=\"color: #0000ff;\">-&gt; In diesem \u00a0Bereich werden sowohl Inhalt als auch Formatierung gel\u00f6scht.<\/span><\/p>\n<h4>Sonstiges<\/h4>\n<ul>\n<li>Fett: <em><span style=\"color: #008000;\">.Font.Bold = True<\/span><\/em> nicht mehr fett:\u00a0<em><em><span style=\"color: #008000;\">.Font.Bold = False<\/span><\/em><\/em><\/li>\n<li>Kursiv: <em><span style=\"color: #008000;\">.Font.Italic = True<\/span><\/em> nicht mehr kursiv:\u00a0<em><em><span style=\"color: #008000;\">.Font.Italic = False<\/span><\/em><\/em><\/li>\n<li>Schriftgr\u00f6\u00dfe: <em><span style=\"color: #008000;\">Font.Size\u00a0<\/span><\/em><\/li>\n<li>Schriftart: <em><span style=\"color: #008080;\">Font.Name<\/span><\/em><\/li>\n<li>Zeilenh\u00f6he festlegen:\u00a0<span style=\"color: #008000;\"><em>Rows<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#8222;1:1&#8220;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>RowHeight <span class=\"token operator\">=<\/span> <span class=\"token number\">30<\/span><\/em><\/span><\/li>\n<li>Spaltenbreite setzen:<em><span style=\"color: #339966;\">\u00a0Columns(&#8222;A&#8220;).ColumnWidth = 30<\/span><\/em> oder\u00a0<em><em><span style=\"color: #339966;\">\u00a0Columns(&#8222;A:G&#8220;).ColumnWidth = 15 <\/span><\/em><\/em><span style=\"color: #339966;\"><span style=\"color: #000000;\">oder<\/span><\/span><em><em><span style=\"color: #339966;\">\u00a0Columns<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#8222;A:B&#8220;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>Autofit<\/span><\/em><\/em><\/li>\n<\/ul>\n<h4>Beispiele<\/h4>\n<ul>\n<li><code class=\"language-visual-basic\">Range<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"a1\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<em><span style=\"color: #008000;\">Font.Bold = True<\/span><\/em><\/span><\/code><\/li>\n<li><code class=\"language-visual-basic\">Range<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"a1\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<em><span style=\"color: #008000;\">Font.italic = True (<\/span><\/em><span style=\"color: #000000;\"><strong>kursiv<\/strong><em>)<br \/>\n<\/em><\/span><\/span><\/code><\/li>\n<li><code class=\"language-visual-basic\">Range<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"a1\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">. <em><span style=\"color: #008000;\">Font.Size <\/span><\/em>= 26<br \/>\n<\/span><\/code><\/li>\n<li><code class=\"language-visual-basic\">Range<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"a1\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<em><span style=\"color: #008080;\">Font.Name<\/span><\/em> =\"Arial\"<\/span><\/code><\/li>\n<\/ul>\n<h3>Zellen zentrieren und ausrichten<\/h3>\n<p>Erst werden die Zellen selektiert:<\/p>\n<p><em><strong><span style=\"color: #008080;\">Range(&#8222;a15:F16&#8220;).Select<\/span><\/strong><\/em><\/p>\n<p>Dann werden die selektierten Zellen ausgerichtet zum Beispiel:<\/p>\n<p><span style=\"color: #008000;\"><em><strong>\u00a0 With Selection<\/strong><\/em><\/span><br \/>\n<span style=\"color: #008000;\"><em><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .HorizontalAlignment = xlCenter<\/strong><\/em><\/span><br \/>\n<span style=\"color: #008000;\"><em><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .VerticalAlignment = xlBottom<\/strong><\/em><\/span><br \/>\n<span style=\"color: #008000;\"><em><strong>\u00a0\u00a0\u00a0 End With<\/strong><\/em><\/span><\/p>\n<p>Im Anschluss werden die selektierten Zellen verbunden:<br \/>\n<span style=\"color: #008080;\"><em><strong>Selection.Merge<\/strong><\/em><\/span><\/p>\n<h2>Tabellenbl\u00e4tter benennen<\/h2>\n<p>Das aktive <em><strong>Tabellenblatt<\/strong><\/em> wird mit\u00a0<span style=\"color: #008000;\">ActiveSheet.\u00a0<\/span>angesprochen. Mit\u00a0<span style=\"color: #008000;\">ActiveSheet.Name = &#8220; Mein Name&#8220;<\/span> wird dann der Name festgelegt.<\/p>\n<h2>Rechnen in VBA<\/h2>\n<p>Wie Du sicher wei\u00dft, geeignet\u00a0\u00a0Excel (und somit auch VBA) sehr gut zum rechnen. Dazu musst Du lediglich einer Zelle den Wert (Ergebnis) zuweisen.<\/p>\n<h6><span style=\"color: #ff00ff;\">Addition<\/span>:<\/h6>\n<p>Beispiel: In <em><strong><span style=\"color: #008000;\">D10<\/span><\/strong><\/em> soll nun das Ergebnis der Addition von <em><strong><span style=\"color: #339966;\">B10<\/span><\/strong><\/em> + <span style=\"color: #008080;\"><em><strong>C10<\/strong><\/em><\/span><br \/>\n<em><strong><span style=\"color: #008080;\">Cells(10,4).value =\u00a0Cells(10,3).value <span style=\"color: #ff00ff;\">+<\/span>\u00a0Cells(10,2).value<\/span><\/strong><\/em><br \/>\n<em><strong><span style=\"color: #008080;\">Cells(10,4) =\u00a0Cells(10,3)\u00a0<span style=\"color: #ff00ff;\">+<\/span>\u00a0Cells(10,2) <\/span><\/strong><\/em><span style=\"color: #008080;\"><span style=\"color: #800000;\">funktioniert auch.<\/span><\/span><\/p>\n<h6><span style=\"color: #ff00ff;\">Subtraktion<\/span>:<\/h6>\n<p>Beispiel: In <em><strong><span style=\"color: #008000;\">D10<\/span><\/strong><\/em> soll nun das Ergebnis der Addition von <em><strong><span style=\"color: #339966;\">B10<\/span><\/strong><\/em>\u00a0&#8211;\u00a0<span style=\"color: #008080;\"><em><strong>C10<\/strong><\/em><\/span><br \/>\n<em><strong><span style=\"color: #008080;\">Cells(10,4) =\u00a0Cells(10,3) <\/span><span style=\"color: #ff00ff;\"><span style=\"caret-color: #ff00ff;\">&#8211;<\/span><\/span><span style=\"color: #008080;\">\u00a0Cells(10,2)<\/span><\/strong><\/em><\/p>\n<p><span style=\"color: #ff00ff;\"><em><strong>Multiplikation\/Division<\/strong><\/em><\/span> funktioniert mit den Rechenzeichen <em><strong><span style=\"color: #ff00ff;\">*<\/span><\/strong><\/em> bzw. <em><strong><span style=\"color: #ff00ff;\">\/<\/span><\/strong><\/em>.<\/p>\n<p><span style=\"color: #ff00ff;\"><em><strong>Potenzieren<\/strong><\/em><\/span>\u00a0funktioniert mit den Rechenzeichen <span style=\"color: #ff00ff;\"><span style=\"caret-color: #ff00ff;\"><b><i>^<\/i><\/b><\/span><\/span>\u00a0;<br \/>\nBsp.: 2^3 = 8 \u00a0\u00a0(2 hoch 3)<\/p>\n<p><span style=\"color: #ff00ff;\"><em><strong>Wurzel ziehen<\/strong><\/em><\/span>\u00a0funktioniert mit den Rechenzeichen <span style=\"color: #ff00ff;\"><span style=\"caret-color: #ff00ff;\"><b><i>^<\/i><\/b><\/span><\/span>\u00a0;<br \/>\nBsp.: 8^(1\/3) = 2 \u00a0-&gt; hier die 3. Wurzel aus 8;<br \/>\n-&gt; <em><strong>^\u00a01\/n<\/strong><\/em>-&gt; n-te Wurzel<\/p>\n<h2>Eingabe der Werte durch eine Inputbox<\/h2>\n<p>Durch den Befehl <span style=\"color: #000000;\"><span style=\"color: #008000;\"><em><strong>InputBox(&#8222;bitte geben Sie die erste Zahl ein&#8220;)<\/strong><\/em><\/span>\u00a0<span style=\"caret-color: #008000;\">erscheint diese Box.<\/span><\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-8601\" src=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/Bildschirmfoto-2023-08-27-um-17.31.38-300x115.png\" alt=\"Bildschirmfoto 2023-08-27 um 17.31.38\" width=\"300\" height=\"115\" srcset=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/Bildschirmfoto-2023-08-27-um-17.31.38-300x115.png 300w, http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/Bildschirmfoto-2023-08-27-um-17.31.38.png 492w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"font-weight: 400;\"><strong>\u00a0<\/strong>Durch den Befehl<strong> <span style=\"color: #008000;\"><em>cells(10,2) =\u00a0I<\/em><\/span><span style=\"color: #000000;\"><span style=\"color: #008000;\"><em><strong>nputBox(&#8222;bitte geben Sie die erste Zahl ein&#8220;) <\/strong><\/em><\/span><\/span><\/strong><span style=\"color: #000000;\">wird die Benutzereingabe (also die eingegebene Zahl) zus\u00e4tzlich in die Zelle B10 geschrieben.<\/span><\/p>\n<h2>Ausgabe der Werte durch eine Messagebox<\/h2>\n<p style=\"font-weight: 400;\"><strong>\u00a0<\/strong>Durch den Befehl <span style=\"color: #008000;\"><em><strong>MsgBox &#8222;Das Ergebnis lautet &#8220; &amp; Cells(10, 4) \u00a0<\/strong><\/em><span style=\"color: #000000;\">wird der Wert der Zelle D10 in einer Messagebox ausgegeben.<\/span><\/span><\/p>\n<p style=\"font-weight: 400;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8604\" src=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/Bildschirmfoto-2023-08-27-um-17.37.55.png\" alt=\"Bildschirmfoto 2023-08-27 um 17.37.55\" width=\"269\" height=\"189\" \/><\/p>\n<p><span style=\"color: #008000;\"><em><strong>MsgBox &#8222;Heute ist der &#8220; &amp; Date&#8220;<\/strong><\/em><\/span><br \/>\n&#8211; &gt; Es wird eine Messagebox mit den heutigen Datum erzeugt<\/p>\n<h2>Deklaration (=Festlegen) von Variablen<\/h2>\n<p>Bisher haben zu immer wenn wir Daten, die der Benutzer eingegeben hat (zum Bsp.: Schriftgr\u00f6\u00dfe, Schriftfarbe, etc. ) verarbeiten wollten, in eine Zelle geschrieben und haben dann durch den Cells bzw. Range-Befehlt darauf zugegriffen.<\/p>\n<p>Dies geschieht in der Informatik i.d.R. einfacher. Es wird eine Variable definiert, in die dieser Wert dann reingeschrieben wird.<\/p>\n<p><strong>VBA-Syntax: <\/strong>\u00a0<span style=\"color: #ff6600;\">Dim <em>VariablenName<\/em> as\u00a0&lt;Datentyp&gt;<\/span><\/p>\n<p><strong>Beispiele<\/strong>:<br \/>\n<span style=\"color: #ff6600;\">Dim schriftgroesse \u00a0as integer<span style=\"color: #0000ff;\">\u00a0-&gt;Variable wird definiert<br \/>\n<span style=\"color: #ff6600;\">schriftgroesse=\u00a0InputBox(&#8222;bitte geben Sie die Schriftgr\u00f6\u00dfe ein!&#8220;)\u00a0<\/span><br \/>\n-&gt;Schriftgr\u00f6\u00dfe wird durch den Benutzer eingegeben und\u00a0in die Variable geschrieben<br \/>\n<span style=\"color: #ff6600;\">cells(1,1).Font.Size =\u00a0schriftgroesse<br \/>\n<span style=\"color: #0000ff;\">-&gt; Die Schrift in der Zelle A1 wird auf diese Gr\u00f6\u00dfe gesetzt.<\/span><\/span><\/span><\/span><\/p>\n<p>Zur Festlegung (Deklaration) von Variablen muss der <span style=\"color: #339966;\"><strong>Name<\/strong><\/span> und der <span style=\"color: #008000;\"><strong>Datentyp<\/strong><\/span> festgelegt werden.<\/p>\n<p>G\u00e4ngige Datentypen sind u.a.\u00a0<em><strong>Integer<\/strong><\/em> (ganze Zahl),\u00a0<em><strong>Double<\/strong><\/em> ( Zahl mit Nachkommastellen),\u00a0<em><strong>Date<\/strong><\/em> (im Format MM\/TT\/YYYY) und\u00a0<em><strong>String<\/strong><\/em> (Text, d.h. beliebige Zeichen<\/p>\n<p><strong>Beispiele<\/strong>:<br \/>\n<span style=\"color: #ff6600;\">Dim KerstinsZahl as integer<br \/>\nDim KerstinsName as\u00a0String<\/span><br \/>\n<span style=\"color: #ff6600;\">Dim KerstinsNachkommaZahl as Double<\/span><br \/>\n<span style=\"color: #ff6600;\">Dim KerstinsGeburtsdatum as\u00a0Date<\/span><\/p>\n<h2>Zuordnen von Werten in Zellen<\/h2>\n<p>Dim laufzeit as Integer<br \/>\nlaufzeit = InputBox(&#8222;Wie lange m\u00f6chten Sie den Kredit abbezahlen?&#8220;)<br \/>\nRange(&#8222;c5&#8220;).Value = laufzeit<br \/>\n-&gt; Es wird durch eine <em><strong>Inputbox<\/strong><\/em> die l<em><strong>aufzeit<\/strong><\/em> abgefragt, in der Variablen <em><strong>laufzeit<\/strong><\/em> gespeichert und in die Zelle C5 geschrieben.<\/p>\n<p>&#8230; oder mit Variablen<br \/>\nDim Zeile, Spalte as integer<br \/>\nZeile =\u00a05<br \/>\nSpalte=3<br \/>\n<span lang=\"EN-US\">Cells(Zeile, Spalte ).Value = &#8222;Laufzeit&#8220;<br \/>\n-&gt; in C5 wird der Wert reingeschrieben.<\/span><\/p>\n<h2>Zuordnen von Werten in Variablen<\/h2>\n<p>Spalte = Spalte +1<br \/>\n-&gt; Die Variable Spalte wird um eins hochgez\u00e4hlt.<\/p>\n<p>Jahr=Year(Date)<br \/>\n-&gt; Die Variable Jahr wird auf das Jahr des aktuellen Datums gesetzt.<\/p>\n<p>Dim ergebnis, zahl As Double<br \/>\nDim exponent As Integer<br \/>\nexponent = 3<br \/>\nzahl =2<br \/>\nergebnis = zahl ^exponent<br \/>\n-&gt; In der Variablen ergebnis steht nun 8 (2 hoch 3)<\/p>\n<h2>Selektieren von Zellen \/ Bereichen<\/h2>\n<p>Oft m\u00fcssen Zellen bzw. Bereiche selektiert (=markiert) werden, um sie zu formatieren:<\/p>\n<h4><em><strong>Beispiele:<\/strong><\/em><\/h4>\n<p><span style=\"color: #ff6600;\">Range(&#8222;C6&#8220;).Select<\/span><br \/>\n<span style=\"color: #ff6600;\">Cells(2,2).Select<\/span><\/p>\n<h4>Beispiel f\u00fcr das Selektieren von Bereichen mit Variablen:<\/h4>\n<p><span style=\"color: #ff6600;\">Dim zeileA, zeileE, spalteA, spalteE As Integer<\/span><br \/>\n<span style=\"color: #ff6600;\"> zeileA = 5<\/span><br \/>\n<span style=\"color: #ff6600;\"> zeileE = 10<\/span><br \/>\n<span style=\"color: #ff6600;\"> spalteA = 2<\/span><br \/>\n<span style=\"color: #ff6600;\"> spalteE = 4<\/span><br \/>\n<span style=\"color: #ff6600;\">Range(Cells(zeileA, spalteA), Cells(zeileE, spalteE)).Select<\/span><br \/>\n<span style=\"color: #0000ff;\">-&gt; Der Bereich von B5 bis D10 wird selektiert<\/span><\/p>\n<h2>Formatieren &#8211; Beispiele<\/h2>\n<p><a href=\"https:\/\/www.excel-inside.de\/beispiele_vba\/vba-formatierung\/270-parameter-fuer-den-vba-befehl-qformatq\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6719\" src=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/04\/info-300x300.png\" alt=\"info\" width=\"74\" height=\"74\" srcset=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/04\/info-300x300.png 300w, http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/04\/info-150x150.png 150w, http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/04\/info.png 600w\" sizes=\"auto, (max-width: 74px) 100vw, 74px\" \/>https:\/\/www.excel-inside.de\/beispiele_vba\/vba-formatierung\/270-parameter-fuer-den-vba-befehl-qformatq<\/a><\/p>\n<p>Mit dem Befehl <em><strong><span style=\"color: #ff00ff;\">Format<\/span><\/strong><\/em> k\u00f6nnen Felder formatiert werden.<br \/>\nParameter f\u00fcr den VBA-Befehl &#8222;Format&#8220;<\/p>\n<p>Syntax: <span style=\"color: #ff00ff;\"><em><strong>FORMAT<\/strong><\/em><\/span>(wert, Format)<\/p>\n<h4>Beispiele:<\/h4>\n<p>Zahlenformate<\/p>\n<p><strong><span style=\"color: #008080;\">dim wert as double<\/span><\/strong><br \/>\n(wert ist hier eine Variable, in der eine Zahl steht)<\/p>\n<ul>\n<li><strong><span style=\"color: #008000;\">FORMAT(wert, &#8222;0.00&#8220;)<\/span><\/strong>-&gt;\u00a0Zahl mit 2 Nachkommastellen<\/li>\n<li><strong><span style=\"color: #008000;\">FORMAT(wert, &#8222;#,##0.00&#8220;)<\/span> <\/strong>-&gt;\u00a0 4.711,00; # Platzhalter f\u00fcr eine Ziffer. Diese wird aber nur angezeigt, wenn sich an dieser Stelle auch wirklich eine Ziffer befindet.<\/li>\n<li><strong><span style=\"color: #008000;\">FORMAT(wert, &#8222;currency&#8220;)<\/span><\/strong> -&gt;\u00a0Liefert das voreingestellte W\u00e4hrungsformat 4.711,00 \u20ac<\/li>\n<li><span style=\"color: #008080;\"><strong>FORMAT(wert, &#8222;0.0 %&#8220;)<\/strong> <\/span>-&gt; % Wandelt die Zahl in Prozent um, indem es die Zahl mit 100 multipliziert und ein %-Zeichen anh\u00e4ngt 471,1 % =<\/li>\n<\/ul>\n<h5><\/h5>\n<p>Eine andere M\u00f6glichkeit Zellen zu formatieren besteht darin, sie zu selektieren und ihnen dann Formatierungen zu zuweisen.<\/p>\n<h4>Beispiele<\/h4>\n<p><span style=\"color: #ff6600;\">Range(Cells(zeileA, spalteA), Cells(zeileE, spalteE)).Select<br \/>\n<\/span><span style=\"color: #ff6600;\">Selection.Font.Bold = True<br \/>\n<\/span><span style=\"color: #ff6600;\">Selection.NumberFormat = &#8222;0_ ;-0 &#8222;<\/span><\/p>\n<p><span style=\"color: #0000ff;\">-&gt; Es wird ein Bereich selektiert und dann fett \u00a0und als Zahl ohne Nachkommastellen formatiert.<\/span><\/p>\n<p><span style=\"color: #0000ff;\">oder Bereich selektieren, fett und zentriert<\/span><br \/>\n<span style=\"color: #ff6600;\">Range(&#8222;B9:F9&#8220;).Select <\/span><br \/>\n<span style=\"color: #ff6600;\"> Selection.Font.Bold = True <\/span><br \/>\n<span style=\"color: #ff6600;\"> With Selection .HorizontalAlignment = xlCenter <\/span><br \/>\n<span style=\"color: #ff6600;\"> End With<\/span><br \/>\n<span style=\"color: #0000ff;\">&#8230; als <strong>Euro<\/strong>-Wert:<\/span><br \/>\n<span style=\"color: #ff6600;\">Cells(zeile, spalte).Select I selektieren des Bereichs<\/span><br \/>\n<span style=\"color: #ff6600;\"> Selection.NumberFormat = _ &#8222;_-* #,##0.00 [$\u20ac-de-DE]_-;-* #,##0.00 [$\u20ac-de-DE]_-;_-* &#8222;&#8220;-&#8222;&#8220;?? [$\u20ac-de-DE]_-;_-@_-&#8220;\u00a0<\/span><\/p>\n<p><span style=\"color: #0000ff;\">oder als <strong>%-Wert<\/strong>\u00a0<\/span><em><strong><br \/>\n<\/strong><\/em><span style=\"color: #ff6600;\">Range(&#8222;C6&#8220;).Select<em><strong><br \/>\nSelection.Style = &#8222;Percent&#8220;<br \/>\nSelection.NumberFormat = &#8222;0.00%&#8220;<\/strong><\/em><\/span><em><strong><br \/>\n<\/strong><\/em><span style=\"color: #0000ff;\">oder\u00a0als ganze <strong>Zahl ohne Tausenderpunkt<\/strong><\/span><br \/>\n<span style=\"color: #ff6600;\">Selection.NumberFormat = &#8222;0_ ;-0 &#8222;<\/span><\/p>\n<p>_<a href=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/VBA-syntax-Info.pdf\">VBA-syntax-Info<\/a>_________________________________________________<\/p>\n<h2>Bedingungspr\u00fcfungen (If&#8230;)<\/h2>\n<p>Es werden 2 F\u00e4lle unterschieden:<\/p>\n<p><span lang=\"EN-US\" style=\"color: #ff6600;\">If Cells(1 , 1 ).Value &lt; 0<br \/>\nThen Cells(1 , 1 ).Interior.ColorIndex = 38 <span style=\"color: #ff00ff;\">(pink)<\/span><br \/>\nElse Cells(1 , 1 ).Interior.ColorIndex = 48 <span style=\"color: #808080;\">(grau)<br \/>\n<span style=\"color: #ff6600;\">End If<\/span><\/span><\/span><span lang=\"EN-US\" style=\"color: #ff6600;\"><span style=\"color: #808080;\"><br \/>\n&#8211;<span style=\"color: #0000ff;\">&gt; In Abh\u00e4ngigkeit des Wertes der Zelle A1 wird der Hintergrund <span style=\"color: #ff00ff;\">pink<\/span> oder <span style=\"color: #808080;\">grau<\/span>.<\/span><\/span><\/span><\/p>\n<p>Bei mehr F\u00e4llen:<\/p>\n<p><strong>Syntax<\/strong>:<\/p>\n<p>If condition<br \/>\n[ Then ] [ statements ]<br \/>\n[ ElseIf elseifcondition [ Then ]<br \/>\n[ elseifstatements ] ]<br \/>\n[ Else [ elsestatements ] ]<br \/>\nEnd If<\/p>\n<p><strong>Beispiele<\/strong>:<br \/>\nIn Abh\u00e4ngigkeit der Zahl in der Zelle A1, wird ihre Hintergrundfarbe gesetzt:<br \/>\n<span style=\"color: #ff6600;\">If Cells(1 , 1 ).Value &lt; 0<\/span>\u00a0<span style=\"color: #ff6600;\">Then<br \/>\n<\/span><span style=\"color: #ff6600;\">Cells(1 , 1 ).Interior.ColorIndex = 38 (<span style=\"color: #ff00ff;\">pink<\/span>)<br \/>\nelseif\u00a0Cells(1 , 1 ).Value &lt;10<\/span>\u00a0<span style=\"color: #ff6600;\">Then<br \/>\nCells(1 , 1 ).Interior.ColorIndex = 50 (<span style=\"color: #339966;\">gr\u00fcn<\/span>)<\/span><br \/>\n<span style=\"color: #ff6600;\">Else<br \/>\nCells(1 , 1 ).Interior.ColorIndex = 48 (<span style=\"color: #999999;\">grau<\/span>)<br \/>\nEnd If<\/span><\/p>\n<p><span class=\"hljs-keyword\">If<\/span> performance = <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">Then<\/span><br \/>\nBonus = salary * <span class=\"hljs-number\">0.1<\/span><br \/>\n<span class=\"hljs-keyword\">ElseIf<\/span> performance = <span class=\"hljs-number\">2<\/span> <span class=\"hljs-keyword\">Then<\/span><br \/>\nBonus = salary * <span class=\"hljs-number\">0.09<\/span><br \/>\n<span class=\"hljs-keyword\">ElseIf<\/span> performance = <span class=\"hljs-number\">3<\/span> <span class=\"hljs-keyword\">Then<\/span><br \/>\nBonus = salary * <span class=\"hljs-number\">0.07<br \/>\n<\/span> <span class=\"hljs-keyword\">Else<\/span><br \/>\nBonus = <span class=\"hljs-number\">0<\/span><br \/>\n<span class=\"hljs-keyword\">End<\/span> <span class=\"hljs-keyword\">If<\/span><\/p>\n<p>-&gt; Die If-Bedingung (entspricht der WENN-Funktion) kann viele F\u00e4lle enthalten.<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/de-de\/dotnet\/visual-basic\/language-reference\/statements\/if-then-else-statement\" target=\"_blank\">https:\/\/docs.microsoft.com\/de-de\/dotnet\/visual-basic\/language-reference\/statements\/if-then-else-statement<\/a><\/p>\n<h2>Schleifen<\/h2>\n<h4>For-Schleife &#8230;<\/h4>\n<p><span class=\"hljs-keyword\">Syntax:<br \/>\nFor<\/span> counter [ <span class=\"hljs-keyword\">As<\/span> datatype ] = start <span class=\"hljs-keyword\">To<\/span> <span class=\"hljs-keyword\">end<\/span> [ <span class=\"hljs-keyword\">Step<\/span> <span class=\"hljs-keyword\">step<\/span> ] [ statements ] [ <span class=\"hljs-keyword\">Continue<\/span> <span class=\"hljs-keyword\">For<\/span> ] [ statements ] [ <span class=\"hljs-keyword\">Exit<\/span> <span class=\"hljs-keyword\">For<\/span> ] [ statements ] <span class=\"hljs-keyword\">Next<\/span> [ counter ]<br \/>\n<a href=\"https:\/\/docs.microsoft.com\/de-de\/dotnet\/visual-basic\/language-reference\/statements\/for-next-statement\" target=\"_blank\">https:\/\/docs.microsoft.com\/de-de\/dotnet\/visual-basic\/language-reference\/statements\/for-next-statement<\/a><\/p>\n<p><span style=\"color: #ff6600;\"><span style=\"text-decoration: underline; color: #000000;\"><strong>Beispiel<\/strong><\/span><br \/>\nDim zeile, spalte, ende, zahl As Integer<\/span><br \/>\n<span style=\"color: #ff6600;\">zeile = 5<\/span><br \/>\n<span style=\"color: #ff6600;\">spalte = 2<\/span><br \/>\n<span style=\"color: #ff6600;\">ende = 10<\/span><br \/>\n<span style=\"color: #ff6600;\">For zahl = 0 To ende<\/span><br \/>\n<span style=\"color: #ff6600;\">Cells(zeile, spalte).Value = zahl<\/span><br \/>\n<span style=\"color: #ff6600;\">Cells(zeile, spalte).Interior.ColorIndex = 1 + zahl<\/span><br \/>\n<span style=\"color: #ff6600;\">zeile = zeile + 1<\/span><br \/>\n<span style=\"color: #ff6600;\">Next<br \/>\n<span style=\"color: #0000ff;\">-&gt; Von Zeile 5 bis 10 in Spalte B wird der Zellenhintergrundver\u00e4ndert.\u00a0<\/span><\/span><\/p>\n<p>Beispiel:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-9017\" src=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/10x10-Farbe-300x296.png\" alt=\"10x10-Farbe\" width=\"300\" height=\"296\" srcset=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/10x10-Farbe-300x296.png 300w, http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-content\/uploads\/2021\/05\/10x10-Farbe.png 560w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Sub farbe10x10()<br \/>\nRange(&#8222;B3&#8220;, &#8222;Z54&#8220;).clear<br \/>\nDim z, sp, zae1, zae2, farbe As Integer<br \/>\nsp = 2<br \/>\nFor zae1 = 1 To 10<br \/>\nz = 3<br \/>\nFor zae2 = 1 To 10<br \/>\nfarbe = Round(Rnd * 56, 0)<br \/>\nCells(z, sp).Interior.ColorIndex = farbe<br \/>\nCells(z, sp).Value = farbe<br \/>\nz = z + 1<br \/>\nNext<br \/>\nsp = sp + 1<br \/>\nNext<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><span class=\"hljs-keyword\">While-Schleife<br \/>\n<\/span><\/h4>\n<p><span class=\"hljs-keyword\">Syntax:<br \/>\nWhile<\/span> condition [ statements ] [ <span class=\"hljs-keyword\">Continue<\/span> <span class=\"hljs-keyword\">While<\/span> ] [ statements ] [ <span class=\"hljs-keyword\">Exit<\/span> <span class=\"hljs-keyword\">While<\/span> ] [ statements ] <span class=\"hljs-keyword\">End<\/span> <span class=\"hljs-keyword\">While<\/span><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/de-de\/dotnet\/visual-basic\/language-reference\/statements\/while-end-while-statement\" target=\"_blank\">https:\/\/docs.microsoft.com\/de-de\/dotnet\/visual-basic\/language-reference\/statements\/while-end-while-statement<\/a><\/p>\n<h4>Do While-Loop<\/h4>\n<p>Syntax:<br \/>\nDo While condition<br \/>\nStatements<br \/>\nLoop<\/p>\n<p><strong>Beispiel:\u00a0<\/strong><\/p>\n<p><span style=\"color: #ff6600;\">Dim zeile, spalte, ende, zahl As Integer<\/span><br \/>\n<span style=\"color: #ff6600;\">zeile = 5<\/span><br \/>\n<span style=\"color: #ff6600;\">spalte = 2<br \/>\nzahl = 1<\/span><br \/>\n<span style=\"color: #ff6600;\">ende = 20<br \/>\n<\/span><br \/>\n<span style=\"color: #ff6600;\">Do While zahl &lt; ende. \u00a0 \u00a0 (<span style=\"color: #0000ff;\">Bedingung)<\/span><\/span><br \/>\n<span style=\"color: #ff6600;\">Cells(zeile+zahl, spalte).Value =zahl \u00a0<span style=\"color: #0000ff;\">(Statements)<\/span><\/span><br \/>\n<span style=\"color: #ff6600;\">zahl = zahl +1<\/span><br \/>\n<span style=\"color: #ff6600;\">Loop\u00a0<\/span><\/p>\n<p>Die Zahl wird von 1 bis 19 hochgez\u00e4hlt und in die Spalte B geschrieben.<\/p>\n<p><em><strong><span style=\"color: #ff00ff;\">x=0<\/span><\/strong><\/em><br \/>\n<em><strong><span class=\"wixui-rich-text__text\" style=\"color: #ff00ff;\">Do<br \/>\n<span class=\"wixui-rich-text__text\"><span class=\"wixui-rich-text__text\"> x = x + 1<br \/>\nCells(x, 1).Value = x<br \/>\nLoop <\/span><\/span><\/span><span class=\"wixui-rich-text__text\"><span class=\"wixui-rich-text__text\"><span class=\"wixui-rich-text__text\"><span style=\"color: #ff00ff;\">Until x &gt; 5<\/span> <\/span><\/span><\/span><\/strong><\/em><\/p>\n<h2>Userforms<\/h2>\n<p><strong>\u00d6ffnen einer Userform: <\/strong>&lt;userform-Name&gt;.show<strong><br \/>\nBsp.:<\/strong> KerstinsUF.show<\/p>\n<h3>Aufrufen von Subs<\/h3>\n<p>Mit<br \/>\n<span style=\"color: #ff6600;\">Call <em><strong>Subname<\/strong><\/em><\/span><br \/>\nk\u00f6nnen innerhalb einer Routine andere aufgerufen werden. Dies bietet sich insbesondere dann an, wenn diese Routine h\u00e4ufiger gebraucht werden.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Achtung &#8220; &#8220; werden hier falsch angezeigt, sie m\u00fcssen immer oben stehen!!!!\u00a0 1. Grunds\u00e4tzliches In VBA (sowie in vielen anderen Programmiersprachen) wird erst das Objekt (zum Beispiel eine Zelle) genannt mit dem etwas passieren soll, dann die Eigenschaft (zum Beispiel Farbe) und dann die konkrete Auspr\u00e4gung (zum Beispiel pink). Beispiel Range(&#8222;E5&#8220;).Font.Color = 1500 oder Cells(6, &hellip; <a href=\"http:\/\/bkovertretung.bplaced.net\/wordpress\/programmieren-in-vba\/vba-syntax-beispiele\/\" class=\"more-link\"><span class=\"screen-reader-text\">VBA-Syntax &#038; Beispiele<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":9122,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6743","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-json\/wp\/v2\/pages\/6743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-json\/wp\/v2\/comments?post=6743"}],"version-history":[{"count":75,"href":"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-json\/wp\/v2\/pages\/6743\/revisions"}],"predecessor-version":[{"id":9185,"href":"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-json\/wp\/v2\/pages\/6743\/revisions\/9185"}],"up":[{"embeddable":true,"href":"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-json\/wp\/v2\/pages\/9122"}],"wp:attachment":[{"href":"http:\/\/bkovertretung.bplaced.net\/wordpress\/wp-json\/wp\/v2\/media?parent=6743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}