Find second empty cell/row and starting pasting from it - excel

I've already did a lot of research and couldn't find a solution. I'm trying to find the second empty cell/row (or other specified cell) in a column and start pasting from it (always jumping to next empty cell/row).
I found the code below, but it starts pasting from the first empty row in a column. Changing the offset command to (2,0) doesn't work too because it finds the second empty row but starts pasting always leaving a empty cell between the collages. And I want to find the second empty cell only in the beginning and from there start pasting always in the next first empty cell/row. Can you guys help me please? Thanks a lot in advance!
For example, I'm copying the range G4:I4 and trying to paste into column G.
Code:
Sub InsertButton()
Range("G4:I4").Copy Range("G" & Rows.Count).End(xlUp).Offset(1, 0)
End Sub

Well, you're trying to write one function that does two different things. If you could include the loop that calls the function, that would be helpful. You could use two functions like this that do each of the things, or pass a parameter to the function that would tell it which thing to do.
Sub InsertButton1()
Range("G4:I4").Copy Range("G" & Rows.Count).End(xlUp).Offset(1, 0)
End Sub
Sub InsertButton2()
Range("G4:I4").Copy Range("G" & Rows.Count).End(xlUp).Offset(2, 0)
End Sub
or
Sub InsertButton(moveDownThisManyRows)
Range("G4:I4").Copy Range("G" & Rows.Count).End(xlUp).Offset(moveDownThisManyRows, 0)
End Sub
Then when you call it say
Sub doStuff()
Dim thisIsTheFirstTime As Boolean
thisIsTheFirstTime = True
For Each item In myStuff
If thisIsTheFirstTime Then
InsertButton2() 'or InsertButton(2)
thisIsTheFirstTime = False
Else
InsertButton1() 'or InsertButton(1)
End If
Next item
End Sub

Related

Trying to run different macros based on value of cell in a range

I am trying to write code that looks at a value, and based on the value ("OPGW" or "Conductor") runs either the OPGW macro or the Conductor macro in another cell further to the right. Then I want it to move down to the next line and do it all again.
So if Cell B8 is OPGW, I want Cell BG8 to run the OPGW code, and then if cell B9 is Conductor, I want cell BG9 to run the Conductor code. There's no problem with the individual macros, though it should be noted that each macro is an exceedingly long formula that SHOULD only take place in the active cell. The only problem I'm having is that it won't go down to the next row and do it all again.
Sub WireUpdate()
N = Cells(Rows.Count, "A").End(xlUp).Row
CR = ActiveCell.Row()
With Range("BG8:BG" & N)
If ActiveSheet.Cells(CR, 2) = "OPGW" Then
Call OPGW2
Else: Call Conductor2
End If
End With
Thank you so much for your help.
I have tried the following code
With Range("BG8:BG" & N)
If ActiveSheet.Cells(CR, 2) = "OPGW" Then
Call OPGW2
Else
If ActiveSheet.Cells(CR, 2) = "Conductor" Then
Call Conductor2
Else: ActiveSheet.Cells(CR, 2) = "0"
End If
End If
End With
I have also tried to make the OPGW and Conductor macros loop, which works, but tends to overwrite the other data. So, it will put in all the OPGW things in, then go through and put all the Conductor things in, overwriting the OPGW things with 0.
I also checked out the following article: Running Different Macros Based on Values in Range but it didn't seem to be what I need, unless it can be adapted in a way I haven't fathomed yet.
This shows one way to loop over a range, and how to call another method based on each cell's value.
Note you need to give the other subs something to work with - you can pass a cell in directly, rather than relying on something like ActiveCell
Sub WireUpdate()
Dim ws As Worksheet, c As Range, n As Long
n = ws.Cells(Rows.Count, "A").End(xlUp).Row
For Each c In ws.Range("BG8:BG" & n).Cells
Select Case c.Value
Case "OPGW": OPGW c '<< call method and pass in the cell
Case Else: Conductor2 c
End Select
Next c
End Sub
Sub OPGW(c As Range)
'do something with c
End Sub
Sub Conductor2(c As Range)
'do something with c
End Sub

programming a serie of words with a loop

I have a macro which checks for each cell of the first line of my 60 first columns, if the cell is empty, the macro automatically fills up the cell with the word “boubou” for every empty cell. However, I would like to fill up the empty cell not with the same word each time. For example, if the macro detects a first empty cell, I would like that it fills up with the word “boubou”, then for the second empty cell found, I would like that it fills up with the word “boubou2”, ”, then for the third empty cell found, I would like that it fills up with the word “boubou3” and so on…
Please find my VBA code below.
If someone knows the solutions, it would be fantastic.
Many thanks
Xavi
Sub hdfhgfdhhgf()
Dim i As Integer
For i = 1 To 60
If IsEmpty(Cells(1, i).Value) = True Then
Cells(1, i).Value = "boubou"
End If
Next i
End Sub
You already had the code, just needed to add & i
Option Explicit
Sub hdfhgfdhhgf()
Dim i As Integer
For i = 1 To 60
If IsEmpty(Cells(1, i)) Then Cells(1, i).Value = "boubou" & i
Next i
End Sub
Thought the first word will be "boubou1"

If statement on Sheet Range

I am writing and Excel VBA if statement, but I can't figure out why it is not working.
I have 1 Sheet called "Data" and I want to check if some variables in column I are the same as in my ActiveSheet row 2, column B (which is number 2). I used the following code which ends automatically because it is not working. Anybody an idea?
Example:
Sub test()
If Sheets("Data").Range("I:I") = ActiveSheet(2, 2) Then
MsgBox ("Yes")
Else
MsgBox ("No")
End If
End Sub
You should create a loop in column I if you want to validate every item in that column; use a flag to bail out as soon as you find a mismatching value, so as to avoid looping through all cells once you already know the outcome:
Dim x as long, result As Boolean
result = True
For x = 1 to 100 'let's say up to row 100
If Worksheets("Data").Range("I" & x).value <> ActiveSheet.Cells(2, 2).value Then
result = False
End If
If Not result Then Exit For
Next x
If result Then
MsgBox "Yes"
Else
MsgBox "No"
End If
You compare a whole column (i.e. 1048576 values) with a single value which obviously does not work. Furthermore if you want to access a specific cell you have to use the Cells-collection of your worksheet i.e. ActiveSheet.Cells(2,2)
If you want to compare each cell in column I individually, use a Loop. If you only want to know if the search-value exists somewhere within the column, you can use the Range.Find method.

Excel VBA - For Each - In & end(xltoright)

So here's what I got so far:
Sub SortByGen()
Dim Gen As Range
For Each Gen In Worksheets("Sheet1").Range("B3:G3")
If Gen.Value = "XXX" Then
Gen.EntireColumn.Copy _
Worksheets("XXX").Range("A2").End(xlToRight).Offset(0, 1).EntireColumn
If Gen.Value = "YYY" Then
Gen.EntireColumn.Copy _
Worksheets("YYY").Range("A2").End(xlToRight).Offset(0, 1).EntireColumn
End If
Next Gen
End Sub
I'm trying to sort certain columns from one worksheet to multiple worksheets based on a certain criterion. I accidentally placed a values on
Worksheets("XXX").Range("B2") and Worksheets("YYY").Range("B2")
so when the
.end(xltoright)
ran, the code worked. Now when I try to get rid of the values in B2, the code will get an error.
If you use xlToRight and you're starting from the last occupied cell in that row then it will go to the very end of the row: you can't offset 1 more column from there...
Use this instead:
Gen.EntireColumn.Copy _
Worksheets("XXX").Cells(2, Columns.Count).End(xlToLeft).Offset(0, 1).EntireColumn

Excel Macro: conditional cut and paste between workbooks

So I want a macro running in an Excel file ("input.xls") that is searching a column in another Excel file ("data.xls") for the value "1" (the only values in that columns are 1s and 0s). When it finds a "1," it should copy and paste the entire row from that file into "input.xls".
Here is the code that I have
Sub NonErrorDataParse()
Dim intEnd As Integer
Workbooks("data.xls").Sheets("Raw").Activate
intEnd = 65000
Range("F").Select
Do Until ActiveCell.Row = intEnd
If Int(ActiveCell.Value) = 1 Then
Range(ActiveCell.Row & ":" & ActiveCell.Row).Cut
intEnd = intEnd - 1
Workbooks("input.xls").Sheets("Non-errors").Activate
Range("A1").Select
ActiveSheet.Paste
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
End Sub
However, when I run it, it gives me a "subscript out of range" error on "data.xls." No matter how I fiddle with the code I can't seem to get past that error (even though I have OTHER macros that are accessing that sheet that work fine).
Any ideas as to how to fix it? Or better code that will do the same thing?
Thanks in advance
You don't have to Select or Activate each time you do a command.
You can also find the last used cell with Range("A65536").End(xlup) instead of parsing every cell (that probably caused your error).
The code would then look like:
Sub NonErrorDataParse()
Dim intEnd As Integer
Dim c As Range
intEnd = Workbooks("data.xls").Sheets("Raw").Range("A65536").End(xlUp).Row
For Each c In Workbooks("data.xls").Sheets("Raw").Range("F1:F" & intEnd)
If CStr(c.Value) = "1" Then
c.EntireRow.Cut
Workbooks("input.xls").Sheets("Non-errors").Rows("1:1").Insert Shift:=xlDown
End If
Next c
End Sub
Yet, if you have many rows, it would be faster to use the autofilter method or use a dictionary.

Resources