In the following code the else condition always happens, even if the two variables are different. If I remove it it works just as it should, but has empty spots where "No" should be written.
For i = 2 To lRowMas 'Loop through all rows in Master Sheet
For j = 2 To lRowCurrentSheet 'Loop through all rows in sheet to compare
If Sheets(masterSheet).Cells(i, 1) = Sheets(sheetNamesAsArray(x)).Cells(j, 1) Then
Sheets(masterSheet).Cells(i, x + 2).Value = "Yes" 'set value to Yes
Else
Sheets(masterSheet).Cells(i, x + 2).Value = "No" 'set value to Yes
End If
Next j 'End inner loop, on sheet to compare
Next i 'End outer loop, on MasterSheet
I solved my problem. I forgot to add a End For, so once it found a match and wrote yes, it would overwrite it with no in the next iteration. Thank you all for your advice.
Related
'I need this to repeat the copy and paste process across columns until counter
'= either key page, "book" cell or reads from row 1, if row 1# = key"book"#
'Next column(Page) should be 3columns from last copy/Pasted column
"PicPg" B2 copies to "PrntPg" B2
E2 to E2
etc
**This is my very first post in any forum to ask for help. Forgive the ignorance.
I'll try and answer any questions the best I can.
Thank you in advanced for your time and help!
I can share the workbook, just not sure how.
Sub createPrintPage()
With Worksheets("PicPg").Cells(2, 2)
.Copy
Sheets("PrntPg").Pictures.Paste(Link:=True).Select
With Worksheets("PrntPg").Cells(2, 5)
.Select
Worksheets("PicPg").Cells(2, 5).Copy
Sheets("PrntPg").Pictures.Paste(Link:=True).Select
End With
End With
End Sub
'the "For i", I have not figured out yet. I have been trying to get it to
'continue repeating.... I've tried to play with for i's... I get lost
'this with statement seems to be working, now to get it to continue across.
'this is day 3 ive researched, Tried many ways... and can only get this far (and this
'is much MUCH prettier(ie:Simplier) then where I began.
both method is used to loop and iterate. if you want to use For just give it a beginning and an ending
Dim i As Integer
For i = 0 To 3
'put your code in here and it will loop 4x (i = 0, i = 1, i = 2 and i = 3)
Next i
'you can put your condition for the loop to exit either at the Do or Loop by using until
i = 0
Do Until i = 3
'although start from 0, but it will loop 3x because when it hit i = 3 it will stop (i = 0, i = 1, i = 2 and i = 3)
i = i + 1 'remember to increment your counter, before leaving the loop the counter had changed to 1
Loop
i = 0
Do
'although start from 0, but it will loop 3x because when it hit i = 3 it will stop (i = 0, i = 1, i = 2 and i = 3)
i = i + 1 'remember to increment your counter
Loop Until i = 3
there is also a lot more different ways to write it.
you can use cell iteration to do your loop
Dim cell As Object 'late binding, early binding can write Dim cell as range
For Each cell In ThisWorkbook.Range("A1:A20")
'do something
Next cell
you can even use your own condition to set the stop
Do
If x = 1 Then
ThisWorkbook.Range("A1").Value = "True"
End If
Loop Until ThisWorkbook.Range("A1") = "True"
you can even Exit Do or Exit For if you have already achieved your desired outcome.
Dim i as Integer, temp as string
For i = 0 To 3
If ThisWorkbook.Range("A" & i).Value2 = "True" Then
temp = "hey, I found what I am looking for"
Exit For
End If
Next i
I am new to macros in Excel, and I’m trying to speed up a process. I need to add a varying number of blank rows, if certain text is present in the cell above it. Not equal, but containing.
For example if A1 contains 'Apples', add two blank rows beneath. If A6 has 'Plums', add four blank rows beneath, etc.
What I have now is this:
For a=1 To ActiveSheet.Cells(Rows.Count,1).End(x1Up).Row
If ActiveSheet.Cells(a,1).Value = “Apples” Then
ActiveSheet.Rows(2).Insert
a = a+1
ELSE
If ActiveSheet.Cells(a,1).Value = “Plums” Then
ActiveSheet.Rows(4).Insert
a = a+1
End If
End Sub
So far I've gotten a Compile Error, stating "Block If without End If" though I believe I closed them both. I'm not sure if I'm correctly comparing or searching for a string as well (referring to my use of ="Apples"), but cannot get it to run at all to test that part.
For a = 1 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
If TypeName(ActiveSheet.Cells(a, 1)) = "String" Then
If ActiveSheet.Cells(a, 1).Value = "Apples" Then
ActiveSheet.Rows(2).Insert
a = a + 1
ElseIf ActiveSheet.Cells(a, 1).Value = "Plums" Then 'One error here
ActiveSheet.Rows(4).Insert
a = a + 1
End If
End If
Next 'And here too
Ok so the thing is, I'm writing this for an Excel Sheet, and the problem i've run into is the loop doesn't check to see if the destination cell is empty or not. So what i'm trying to do is check if the cell is empty if it is then do the existing paste. if it's not then keep looking till it finds the first empty box....can someone help with this?
Dim x, z
Set a = Sheets("Working")
Set b = Sheets("Peer Review")
Set c = Sheets("Waiting to Push")
Set d = Sheets("Completed")
x = 1
z = 2
Do Until IsEmpty(a.Range("I" & z))
If a.Range("I" & z) = "Peer" Then
x = x + 1
b.Rows(x).Value = a.Rows(z).Value
Else
If a.Range("I" & z) = "Waiting" Then
x = x + 1
c.Rows(x).Value = a.Rows(z).Value
End If
End If
z = z + 1
Loop
I'd recommend to rewrite the code as follows:
Option Explicit
Public Sub tmpSO()
Dim z As Long
Dim a As Worksheet, b As Worksheet, c As Worksheet, d As Worksheet
Set a = ThisWorkbook.Worksheets("Working")
Set b = ThisWorkbook.Worksheets("Peer Review")
Set c = ThisWorkbook.Worksheets("Waiting to Push")
Set d = ThisWorkbook.Worksheets("Completed")
z = 2
For z = a.Cells(a.Rows.Count, "I").End(xlUp).Row To 2 Step -1
If a.Cells(z, "I").Value2 <> vbNullString Then
Select Case UCase(a.Cells(z, "I").Value2)
Case "PEER"
b.Rows(b.Cells(b.Rows.Count, "I").End(xlUp).Row + 1).Value2 = a.Rows(z).Value2
a.Rows(z).Delete
Case "WAITING"
c.Rows(c.Cells(c.Rows.Count, "I").End(xlUp).Row + 1).Value2 = a.Rows(z).Value2
a.Rows(z).Delete
Case "COMPLETED"
d.Rows(d.Cells(d.Rows.Count, "I").End(xlUp).Row + 1).Value2 = a.Rows(z).Value2
a.Rows(z).Delete
Case Else
MsgBox "Unknown value " & a.Cells(z, "I").Value2 & " in row " & z & Chr(10) & "Skipping to next row..."
End Select
End If
Next z
End Sub
Changes:
Implement a for ... next instead of a loop is most of the time a better coding practice since a loop can potentially lead to an infinite loop and crash your Excel.
Using select case instead of multiple if clauses. This is not really much faster but simply better to read and understand.
I removed x because this would not always use the last row on each sheet. Instead x is incremented on each sheet and thus can lead to empty (in between) rows on all other sheets. Instead, the above code now checks column I for the last row on that sheet and then copies the row from sheet a over to the next available one.
The above code is now (no longer) case sensitive when checking for peer or Peer or pEEr in column I. I am guessing that this better suits your needs.
If an unknown value (other than peer, waiting, or completed) in column I is encountered then you get a message box telling you about it.
In accordance to your request (in the comments below) the above code now deletes any row which has been successfully copied over to another sheet. Yet, unrecognized values in column I cannot be copied over to any other sheet and (as such) stay on sheet a ("Working").
Note, that the above assumes that "empty" is defined as "there in nothing in the cell's formula. If you prefer you can also set it to "if the cell is showing no value" (instead). The difference is that if a cell contains a formula which results in "" then there is a formula in the cell but the value is currently (due to the formula) nothing.
I'm trying to get excel to put together a series of text strings that haven't been formatted systematically, so that they end up split into different rows on a data sheet.
I'm aware this might've been solved elsewhere so sorry for that but I'm struggling to describe the issue, and I can't post images on it but basically it's
Column 1 with a list of the entries, and
Column 2 with text strings that are spread over 2 or more rows
Is it possible to write some kind of formula or macro that would be able to check the first column and then stitch together all entries in the second column going down until it found a new entry in the first column? I've got a feeling it might be possible using some sort of loop thing with index functions, but I've no idea where to start even.
Thanks,
Mike
Mike give this a ty
Sub appendValues()
'The sub is designed to loop through code and when ever there is a null value and column a it will take the value of what is in column B and appended to the row above it and delete the row.
Dim row As Integer
row = 1
'This code starts with row one but this can be changed at will.
Do Until ThisWorkbook.Sheets("sheet1").Cells(row, 2).Value = ""
'loop statement is designed to continue to Loop until there is a null value inside of you the value in the second column.
If ThisWorkbook.Sheets("sheet1").Cells(row, 1).Value = "" Then
ThisWorkbook.Sheets("sheet1").Cells(row - 1, 2).Value = ThisWorkbook.Sheets("sheet1").Cells(row - 1, 2).Value & ThisWorkbook.Sheets("sheet1").Cells(row, 2).Value
Rows(row).Delete
Else
'else statement is needed because there is an implied looping by decreasing the total number of rows after the delete.
row = row + 1
End If
Loop
End Sub
Sub appendValues()
'The sub is designed to loop through code and when ever there is a null value and column a it will take the value of what is in column B and appended to the row above it and delete the row.
Dim row As Integer
row = 1
'This code starts with row one but this can be changed at will.
Do Until ThisWorkbook.Sheets("sheet1").Cells(row, 2).Value = ""
'loop statement is designed to continue to Loop until there is a null value inside of you the value in the second column.
If ThisWorkbook.Sheets("sheet1").Cells(row, 1).Value = "" Then
ThisWorkbook.Sheets("sheet1").Cells(row - 1, 2).Value = ThisWorkbook.Sheets("sheet1").Cells(row - 1, 2).Value & ThisWorkbook.Sheets("sheet1").Cells(row, 2).Value
Rows(row).Delete
Else
'else statement is needed because there is an implied looping by decreasing the total number of rows after the delete.
row = row + 1
End If
Loop
End Sub
Ive written a macro that takes some source data and writes it onto several sheets, which id like to remain hidden before and after the macro has run. Having written the Macro, when I run it it only updates a few records on each sheet (for instance on the first hidden sheet it updates 21 rows out of over 1000. What is the reason for this happening? Surely it should update them all or none of them? Im not getting any errors either. Ive tried
Application.ScreenUpdating = False
Worksheets("FT Raw").Visible = True
Worksheets("L1").Visible = True
Worksheets("L2").Visible = True
Worksheets("L3").Visible = True
Worksheets("L4").Visible = True
But still only 21 rows get updated.
Update: This is the code that is running on each sheet
endval = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To endval
If Not Sheets("FT Raw").Cells(i, "A") = "" Then
splitted = Split(Sheets("FT Raw").Cells(i, "A"), ",")
Sheets("FT Raw").Cells(i, "B") = splitted(0)
Sheets("FT Raw").Cells(i, "C") = splitted(1)
Sheets("FT Raw").Cells(i, "D") = splitted(2)
If Sheets("FT Raw").Cells(i, "D") = "1" Then
Sheets("L1").Cells(j, "A") = Trim(splitted(0))
Sheets("L1").Cells(j, "B") = Trim(splitted(3))
j = j + 1
End If
End If
Next i
Ok Edit. You need to specify the Sheet("FT Raw") in your endval calc.
Try this set endval = Sheets("FT Raw").Cells(Sheets("FT Raw").Rows.Count, 1).End(xlUp).Row and proceed with the remainder of your code.
(You could also use endval = Sheets("FT Raw").UsedRange.Rows.Count only if you don't have blank cells at the top of the column)
Lucky last, you don't have to unhide these sheets at all to run the code. By all means do so to debug but when running in anger it's not necessary.
This has nothing to do with the hiding of sheets - even if a sheet is hidden, you can still modify it with VBA. (Only if it is protected, you need to unprotect it.)
Lookig at your code, I do not see, where j is initialized - and under which circumstances column D in your sheet FT rawis equal to "1".
I suggest you initialize j in the top, e.g. if you want to add the rows using
j = Sheets("L1").Range(Rows.Count, 1).End.(xlUp).Row + 1
Then, after running the macro (for a check), filter column D in your raw sheet with an Autofilter for the value 1 and see if that matches the number of entries in sheet L1.