New to VBA if someone could help me what im doing wrong here.
Trying to run a loop such that it looks for a specific text, starts the loop then stops at a specific point.
The loops is such that I want it to copy some values below in my sheet hence a is 55.
Im facing the error Block IF without End If
Here is the code:
Private Sub CommandButton3_Click()
For y = 1 To 15 Step 5
Dim x As Double
Dim a As Double
x = 1
a = 55
If Cells(x, y).Value = "Text1" Then
Do Until Cells(x, y).Value = "Text2"
Cells(a, y) = Cells(x, y).Value
Cells(a, y + 1) = Cells(x, y + 1)
x = x + 1
a = a + 1
Loop
End Sub
Indenting is the way forward, you have a for statement with no next and an if with no End If:
Private Sub CommandButton3_Click()
For y = 1 To 15 Step 5
Dim x As Double
Dim a As Double
x = 1
a = 55
If Cells(x, y).Value = "Text1" Then
Do Until Cells(x, y).Value = "Text2"
Cells(a, y) = Cells(x, y).Value
Cells(a, y + 1) = Cells(x, y + 1)
x = x + 1
a = a + 1
Loop
End If
Next y
end sub
Besides the issues I mentioned in the comments to your post, if I understood you correctly, you want to loop on cells at Column A, find the first "Text1", then copy all the cells to row 55 and below, until you find "Text2". If that's the case, try the code below :
Private Sub CommandButton3_Click()
Dim x As Long, y As Long
Dim a As Long
Dim LastRow As Long
With Worksheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
For y = 1 To 15 Step 5
x = 1 '<-- reset x and a (rows) inside the columns loop
a = 55 '<-- start pasting from row 55
LastRow = .Cells(.Rows.Count, y).End(xlUp).Row
While x <= LastRow '<-- loop until last row with data in Column y
If .Cells(x, y).Value Like "Text1" Then
Do Until .Cells(x, y).Value = "Text2"
.Cells(a, y).Value = .Cells(x, y).Value
.Cells(a, y + 1).Value = .Cells(x, y + 1).Value
x = x + 1
a = a + 1
Loop
End If
x = x + 1
Wend
Next y
End With
End Sub
Related
Cant seem to figure out why my code is not showing output. New VBA programmer only know basics so any help would be helpful.
What I want is for Excel to start checking a specific column for a specific text1 and then start copying and pasting those values till it reaches text2. After that I want it to check the next fifth column in the same manner.
If you could suggest modifications to my code.
Without putting in a for loop for the column my code looks like this.
Private Sub CommandButton7_Click()
Dim x As Long, y As Long
Dim a As Long
y = 1 'starts with the first column
x = 1 'first row
a = 70 'this is the row where i want the data to be posted
If Cells(x, y).Value = "text1" Then 'check first for specific text
Do Until Cells(x, y).Value = "text2" 'stop here
Cells(a, y).Value = Cells(x, y).Value 'copy that data to new row
Cells(a, y + 1).Value = Cells(x, y + 1).Value 'and the column adjacent to it
x = x + 1
a = a + 1
Loop
Else
x = x + 1 'if not on that row then check the next row
End If
End Sub
Really hard to see what is going wrong here as your code should be doing what you want.
The only other thing that could throw your results is when you have different case ,as VBA will treat a string with an upper case character as being different, so you may not actually be entering the loop at all. And I am assuming that text1 is just a sample string for the question.
So comparing the string in lower case will ensure that if you have any upper case characters they will be compared correctly, using the LCase function should help with that.
Full code,
Private Sub CommandButton7_Click()
Dim x As Long, y As Long
Dim a As Long
y = 1 'starts with the first column
x = 1 'first row
a = 70 'this is the row where i want the data to be posted
If LCase(Cells(x, y).Value) = LCase("text1") Then 'check first for specific text
Do Until LCase(Cells(x, y).Value) = LCase("text2") 'stop here
Cells(a, y).Value = Cells(x, y).Value 'copy that data to new row
Cells(a, y + 1).Value = Cells(x, y + 1).Value 'and the column adjacent to it
x = x + 1
a = a + 1
Loop
Else: x = x + 1 'if not on that row then check the next row
End If
End Sub
Kind of hard to see the big picture but I think I produced the result you want with:
Sub FindText()
Dim textFound As Range
Dim x As Long, y As Long
Dim a As Long
y = 1 'starts with the first column
x = 0 'first row
a = 70 'this is the row where i want the data to be posted
Set textFound = ActiveSheet.Columns(y).Cells.Find("Text1")
Do Until textFound.Offset(x, 0).Value = "Text2"
Cells(a + x, y).Value = textFound.Offset(x, 0).Value
Cells(a + x, y + 1).Value = textFound.Offset(x, 1).Value
x = x + 1
Loop
End Sub
This code is far from perfect but should work in most circumstances.
My problem is the as follows:
I have 3 columns and 20 rows, that contains numbers.
There is a line with numbers between 1 to 20 in order crescente, the other cells contains bigger numbers then 100 or whatever.
My homework is that I have to write a VBA code which fill color the cells that contains the line. This way i going to have a "colorful snake" from the cells that contains the numbers between 1 to 20.
Of course, the starting number cell is "A1"
the ending cell can be anywhere in the area "A1:C20"
the substance is the colored cells must have follow the numbers in order cresence!
Sub MeykEhYewowSnakhey()
Dim r, c
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
For r = 1 To ws.UsedRange.Rows.Count
For c = 1 To ws.UsedRange.Columns.Count
If ws.Cells(r, c).Value < 100 Then
ws.Cells(r, c).Interior.ColorIndex = 6
End If
Next
Next
End Sub
Try that.
There is probably a much more efficient way to solve this but this is my solution.
Sub Snake()
Dim wbk As Workbook
Dim ws As Worksheet
Dim mySnake As Integer, x As Integer, y As Integer
Set wbk = Workbooks("Book1.xlsm")
Set ws = wbk.Worksheets("Sheet1")
x = 1
y = 1
With ws
For mySnake = 1 To 20
If .Cells(x, y) = mySnake Then
.Cells(x, y).Interior.Color = vbYellow
'Check cell below
If .Cells(x + 1, y) = mySnake + 1 Then
x = x + 1
'Check cell to right
ElseIf .Cells(x, y + 1) = mySnake + 1 Then
y = y + 1
'Check cells to left if y <> 1
ElseIf y <> 1 Then
If .Cells(x, y - 1) = mySnake + 1 Then
y = y - 1
End If
'Check cells above if x <> 1
ElseIf x <> 1 Then
If .Cells(x - 1, y) = mySnake + 1 Then
x = x - 1
End If
End If
End If
Next mySnake
End With
End Sub
So new to coding completely. here is question :
How do I make a code that finds a multiple of a number within a set.
Ex. I have a set of number: I want to order the number beginning with the first number with every pair that is 14 a part. I was able to figure out how to do this (See code below) But now I want to do another code looking for multiples of 14 so.. It would look at x, and then find (x*14), (x*(2*14)), etc.. Any help would be appreciated
Column A Column B
459
452
426
485
425
Sub GetPairs()
Dim x, z As Single
Dim lastrow, pasterow As Single
Dim testMass, nomMass As Single
lastrow = Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Row
pasterow = 2
For x = 2 To lastrow
nomMass = Cells(x, 2).Value
testMass = Cells(x, 2) + 14
o
r z = 2 To lastrow
If Cells(z, 2).Value = testMass Then
Cells(pasterow, 7).Value = nomMass
Cells(pasterow, 8).Value = Cells(z, 2).Value
pasterow = pasterow + 1
End If
Next z
Next x
End Sub
Actually, it should be that simple.
multiple = Cells(x*14, 2)
I think that should do what you want.
Yes That worked perfectly.
Here is the final code I came up with :
Sub GetPairs()
``Dim x As Single, z As Single
Dim lastRow, pasterow As Single
Dim testMass, nomMass As Single
`` Dim lastValue As Long
` Dim colCounter As Long
``Dim lookUpRange As Range
`lastRow = Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Row
`lastValue = Worksheets(1).Cells(Rows.Count, 2).End(xlUp).Value
`Set lookUpRange = Worksheets(1).Range("B2:B" & lastRow)
``pasterow = 2
`For x = 2 To lastRow
nomMass = Cells(x, 2).Value ' base value
colCounter = 3
For z = Round((nomMass + 14), 0) To Round((lastValue + 14), 0) Step 14
If Found(lookUpRange, z) Then
'found
Worksheets(1).Cells(x, colCounter) = z
colCounter = colCounter + 1
End If
Next z
Next x
End Sub
Private Function Found(rng As Range, valueToFind) As Boolean
On Error GoTo errHandler
Dim v
v = WorksheetFunction.VLookup(valueToFind, rng, 1, 0)
Found = True
Literally first time in two decades I've done this and that was even super basic (no pun intended). I have Sheet1 ("Main Page") that I am copying data from ("Control Sequences") based on data entered in Column B. I have it sort of working. The issue I will run into is when I copy over the first set of data, then want to bring in another set, the code runs for the whole sheet again and overwrites any tweaks I did previously. I want to be able to bring in a set of data to Sheet1, manually skip a couple of rows, type in another value in column B below that, re-run the code and append new data in. I'll try to come up with a simpler explanation if this doesn't make sense. Brain is fried right now after 5 hours of VBA absorption :P Here is the code I have so far in its entirety (It's sort of brute force so beware):
Sub test()
Dim i As Integer 'Main Page Sheet Row Number
Dim n As Integer 'Control Sequences Sheet Row Number
Dim x As Integer 'Main Page Current Row Number
Dim y As Integer 'Column Number
Dim CSrow As Integer 'Current Row
Dim NextCS As Integer 'Next Control Sequence
Dim NextCSrow As Integer 'Row To Stop At
Dim ws1 As Worksheet 'Var
Dim ws2 As Worksheet 'Var
Set ws1 = Worksheets("Main Page")
Set ws2 = Worksheets("Control Sequences")
y = 2
'Cycles through the codes in sheet 1
For i = 2 To ws1.Cells(ws1.Rows.Count, y).End(xlUp).row Step 1
For n = 2 To ws2.Cells(ws2.Rows.Count, y).End(xlUp).row Step 1
If ws1.Cells(i, y).Value = ws2.Cells(n, y).Value Then
x = i
CSrow = ws2.Cells(n, y).row
NextCS = ws1.Cells(i, y).Value + 1
NextCSrow = Application.WorksheetFunction.Match(NextCS, ws2.Range("B1:B200"), 0)
NextCSrow = NextCSrow - 1
For CSrow = CSrow To NextCSrow
y = y + 1
For y = 3 To 7
ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value
Next y
' ws1.Cells(x, 8).Formula = ws2.Cells(CSrow, 8).Formula
y = y + 1
ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value
y = y + 2
ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value
x = x + 1
y = 2
Next CSrow
End If
Next n
Next i
End Sub
Thanks to anyone for your help and input.
EDIT 13 FEB 2014
As mentioned in the comment to the Answer below, I took out the .End(xlUp) piece and it worked. I've also changed the body of the writing loop to this:
For CSrow = CSrow To NextCSrow
' y = y + 1
' For y = 3 To 7
' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value
' Next y
' ws1.Cells(x, 8).Formula = ws2.Cells(CSrow, 8).Formula
' y = y + 1
' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value
' y = y + 2
' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value
' x = x + 1
' y = 2
ws2.Rows(CSrow).Copy Destination:=ws1.Cells(x, 1)
x = x + 1
Next CSrow`
I've got the formatting and the formula to copy over without keeping the original reference :D On to part IV... Testing ALL of the Variables and not just 1 ;) I will continue to update this thread with ... well ... updates.
EDIT 20 FEB 2014
Here's the complete code as it stands now:
Sub test()
Dim i As Long 'Main Page Sheet Row Number
Dim j As Long 'Placeholder
Dim n As Long 'Control Sequences Sheet Row Number
Dim x As Long 'Main Page Current Row Number
Dim y As Long 'Column Number
Dim z As Long
Dim a As Long
Dim CSrow As Long 'Current Row
Dim NextCS As Long 'Next Control Sequence
Dim NextCSrow As Long 'Row To Stop At
Dim ws1 As Worksheet 'Var
Dim ws2 As Worksheet 'Var
Dim ws3 As Worksheet 'Var
Dim ws4 As Worksheet 'Var
' Set ws1 = Worksheets("Main Page")
Set ws1 = ActiveSheet
Set ws2 = Worksheets("Control Sequences")
Set ws3 = Worksheets("Cost 1")
Set ws4 = Worksheets("Cost 2")
If ws1.Name = ws2.Name Or ws1.Name = ws3.Name Or ws1.Name = ws4.Name Then
End
End If
y = 2
z = 10
a = ws1.Cells(ws1.Rows.Count, z).End(xlUp).row + 2
If IsEmpty(ws1.Cells(a, y).Value) Then End
'Cycles through the codes in sheet 1
j = ws1.Cells(ws1.Rows.Count, y).End(xlUp).row
i = ws1.Cells(j, y).row
For i = i To j Step 1
For n = 2 To ws2.Cells(ws2.Rows.Count, y).End(xlUp).row Step 1
If ws1.Cells(i, y).Value = ws2.Cells(n, y).Value Then
x = i
CSrow = ws2.Cells(n, y).row
NextCS = ws1.Cells(i, y).Value + 1
NextCSrow = Application.WorksheetFunction.Match(NextCS, ws2.Range("B1:B100"), 0)
NextCSrow = NextCSrow - 1
For CSrow = CSrow To NextCSrow
' y = y + 1
' For y = 3 To 7
' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value
' Next y
' ws1.Cells(x, 8).Formula = ws2.Cells(CSrow, 8).Formula
' y = y + 1
' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value
' y = y + 2
' ws1.Cells(x, y).Value = ws2.Cells(CSrow, y).Value
' x = x + 1
' y = 2
ws2.Rows(CSrow).Copy Destination:=ws1.Cells(x, 1)
x = x + 1
Next CSrow
End If
Next n
Next i
End Sub
I added a check that if the user was on any of the "Template" sheets, the code would just stop. It's a bit brute force, but it gets the job done and it's the only code I have. Maybe if I continue to do this, I'll try to get more "streamlined". :D Thanks to everyone for their input and help.
I think I have it. Your problem is in the first line of your loop:
For i = 2 To ws1.Cells(ws1.Rows.Count, y).End(xlUp).row Step 1
Try setting i dynamically before the loop begins. DIM another variable j for this, then replace the above line with the following:
j = ws1.Cells(ws1.Rows.Count, y).End(xlUp).row
i = ws1.Cells(j, y).End(xlUp).row
For i = i to j Step 1
While you're at it, change your row integers to long since there are more rows in a worksheet than integers can handle.
I'm working on a project where I have sales data broken down into quarters. What I need to do is in front of each column insert the three months that belong in that quarter. I started with a select case statement, but then realized that probably isn't the best way to do it. What I want to do is have it be a variable range (there can be anything from 1-10 years pasted in) so I set it up to search InStr for "Q1", "Q2" and then insert the rows and proper month titles. I haven't inserted month titles yet, because I want to get the rows inserted first, but if you have a suggestion on how to do that without specifying cell values that'd also be awesome! it's also worth mentioning this data insertion starts on column U and will every time. Thanks for any help or suggestions!
Sub InsertMonths()
If cell.value = InStr(1, cell, "Q1", 1) Then
Dim y As String
y = InStr(1, cell, "Q1", 1)
If y = "" Then Exit Sub
Dim x As Long
For x = Cells(Columns.Count, 1).End(xlUp).Column To 1 Step -1
If Cells(x, 18).value = y Then
Columns(x + 3).Resize(1).Insert
End If
Next x
Else cell.value = InStr(1, cell, "Q2", 1) Then
Dim y As String
y = InStr(1, cell, "Q2", 1)
If y = "" Then Exit Sub
Dim x As Long
For x = Cells(Columns.Count, 1).End(xlUp).Column To 1 Step -1
If Cells(x, 18).value = y Then
Columns(x + 3).Resize(1).Insert
End If
Next x
Else InStr(1, cell, "Q3", 1) then
Dim y As String
y = InStr(1, cell, "Q3", 1)
If y = "" Then Exit Sub
Dim x As Long
For x = Cells(Columns.Count, 1).End(xlUp).Column To 1 Step -1
If Cells(x, 18).value = y Then
Columns(x + 3).Resize(1).Insert
End If
Next x
Else InStr(1, cell, "Q4", 1) then
Dim y As String
y = InStr(1, cell, "Q4", 1)
If y = "" Then Exit Sub
Dim x As Long
For x = Cells(Columns.Count, 1).End(xlUp).Column To 1 Step -1
If Cells(x, 18).value = y Then
Columns(x + 3).Resize(1).Insert
End If
Next x
End If
End Sub
Without coming into too much detail in the exact situation, here you have a couple of loops doing the same than your set of conditions. It is prepared to deal with as many cells as required (letters and ints).
Sub InsertMonths()
Dim startInt, endInt, totLetters, lettersCount, curInt As Integer
Dim allLetters(10), curLetter, curCell As String
totLetters = 1
allLetters(1) = "Q"
startInt = 1
endInt = 4
lettersCount = 0
Do
lettersCount = lettersCount + 1
curLetter = allLetters(lettersCount)
curInt = startInt - 1
Do
curInt = curInt + 1
curCell = curLetter & CStr(curInt)
If cell.Value = InStr(1, cell, curCell, 1) Then
Dim y As String
y = InStr(1, cell, curCell, 1)
If y = "" Then Exit Sub
Dim x As Long
For x = Cells(Columns.Count, 1).End(xlUp).Column To 1 Step -1
If Cells(x, 18).Value = y Then
Columns(x + 3).Resize(1).Insert
End If
Next x
End If
Loop While (curInt < endInt)
Loop While (curLetter < totLetters)
End Sub
In your code, where you are setting the value in the cell to hold the month, put the following formula instead of the value
Cells(x, y).value = "=(MID($D2,2,1) - 1) * 3 + 1"
Second column would be
Cells(x, y).value = "=(MID($D2,2,1) - 1) * 3 + 2"
And third would be
Cells(x, y).value = "=(MID($D2,2,1) - 1) * 3 + 3"
In all of the cases above, the $D2 should reference the cell you found to contain the "Q#". The formulas are basically taking the numerical part of the quarter and calculating the 1st, 2nd and 3rd months of the quarter.
Also note that this gives you the month number. If you want the name, you should be able to figure that out.