Add 5 columns of data under another data set - Next Blank Row - excel

I am trying to put two different data sets together and it isn't working for me. Here is the code.
Sheets("Event Data").Select
Range("S:S,T:T,U:U,AA:AA,N:N").Select
Range("Table_Query_from_PostgreSQL35W[[#Headers],[site_name]]").Activate
Selection.Copy
Sheets("Staff List").Select
Range("A1:E1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Anon").Select
Range("A:A,D:D,O:O,P:P,Q:Q").Select
Range("P2").Activate
Selection.Copy
Sheets("Staff List").Select
**Range("A2:E2").Select**
ActiveSheet.Paste
Application.CutCopyMode = False
I know this is not correct. The issue I am having is selecting the first blank row (which I can do for one row) but for multiple rows (A:E). I am looking for a solution quickly. Thank you.

For the next blank row in any of A:E try,
dim nr as long
with workSheets("Staff List")
nr = application.max(.cells(.rows.count, "A").end(xlup).offset(1, 0).row, _
.cells(.rows.count, "B").end(xlup).offset(1, 0).row, _
.cells(.rows.count, "C").end(xlup).offset(1, 0).row, _
.cells(.rows.count, "D").end(xlup).offset(1, 0).row, _
.cells(.rows.count, "E").end(xlup).offset(1, 0).row)
.cells(nr, "A").paste
end with
The above code replaces the following lines of code:
Sheets("Staff List").Select
**Range("A2:E2").Select**
ActiveSheet.Paste

Related

Grouping records in an Excel sheet which have the same values in one column but only one unique record in other columns

Dummy data of a tournament
Above is the example of the dummy data. My goal is to use VBA to group the data so that there is only one name displayed and the 3 Games populated with the Results so there would only be one line for the name as well as the 3 Games' results in the same line.
Example of the output data
Well, this is not as easy as first appears, however, this works:
So, the country is returned with classic index & match. The results are built by finding the result against each player and round. This expects blanks in the other cells for each player.
Try this:
Sub mSummarise()
'
' Macro1 Macro
'
'
Dim lData, lSummary, lFilter As String
Dim lRow1, lRow2, lRow3, lCol1, lCount As Long
lData = ActiveSheet.Name
Range("A1").Select
Selection.End(xlToRight).Select
lCol1 = ActiveCell.Column
Range("A1").Select
Selection.End(xlDown).Select
lRow1 = ActiveCell.Row
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Summary"
Sheets(lData).Activate
Range("A1:B" & lRow1).Select
Selection.Copy
Sheets("Summary").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$B$" & lRow1).RemoveDuplicates Columns:=Array(1, 2), Header _
:=xlNo
Range("A1").Select
Selection.End(xlDown).Select
lRow2 = ActiveCell.Row
Sheets(lData).Select
Range(Cells(1, 3), Cells(1, lCol1)).Select
Selection.Copy
Sheets("Summary").Select
Range("C1").Select
ActiveSheet.Paste
Sheets(lData).Select
For lCount = 3 To lCol1
Range(Cells(1, 1), Cells(lRow1, lCol1)).Select
Selection.AutoFilter
ActiveSheet.Range(Cells(1, 1), Cells(lRow1, lCol1)).AutoFilter Field:=lCount, Criteria1:="<>", Operator:=xlAnd
Range(Cells(1, 1), Cells(lRow1, lCount)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
lFilter = ActiveSheet.Name
ActiveSheet.Paste
Range("A1").Select
Selection.End(xlDown).Select
lRow3 = ActiveCell.Row
Sheets("Summary").Select
Application.CutCopyMode = False
Cells(2, lCount).Select
ActiveCell.Formula = "=VLOOKUP(A2," & lFilter & "!$A$2:" & Cells(lRow3, lCount).Address & "," & lCount & ",0)"
Range(Cells(2, lCount), Cells(2, lCount)).Copy
Range(Cells(2, lCount), Cells(lRow3, lCount)).Select
ActiveSheet.Paste
Range(Cells(2, lCount), Cells(lRow3, lCount)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(lFilter).Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets(lData).Select
Next
Selection.AutoFilter
Range("A1").Select
Sheets("Summary").Select
Range("A1").Select
End Sub

Adding a number of rows to another worksheet based on cell value on a certain column

I am new to VBA, but had a situation where doing this manually would be extremely tedious, so I got to learning.
I needed a script that can find certain text values on a column and then copy a certain number of rows with all the row values into another worksheet. Full row values on the first row, and first 5 rows on the next rows. The text value that is searched is for example "DOL-1" or "VFD".
After lots of research and trial and error, I have managed to stitch together this script that does the job, but it is obviously badly written and not optimized. I have tried searching for similar questions and tried their answers, but I couldn't get anything to do what this script does.
I was wondering if there are some better and/or faster methods to achieve the same thing as this script does?
Sub Add_Rows()
Dim wbC As Workbook
Dim wbP As Workbook
Dim wsC As Worksheet
Dim wsP As Worksheet
Dim cell As Range
Dim r As Integer
Dim dataTable As Range
r = 8
'rownumber
Set wbP = Application.Workbooks.Open("C:\Projects\Feed_list.xlsx")
Set wsP = wbP.Worksheets("Feed_list")
' set paste destination (these variables aren't really even used because I couldn't get them to work)
Set wbC = Application.Workbooks.Open("C:\Projects\Generated_list.xlsm")
Set wsC = wbC.Worksheets("GEN")
' set copy location (these variables aren't really even used because I couldn't get them to work)
Windows("Generated_list.xlsm").Activate
Application.ScreenUpdating = False
For Each cell In Range("AB2:AB5000")
If cell.Value = "DOL-1" Then
Debug.Print cell.Address
Windows("Generated_list.xlsm").Activate
Range(cell, cell.Offset(, -25)).Copy
Windows("Feed_list.xlsx").Activate
Sheets("Feed_list").Select
'Debug.Print r
Rows(r).Select
Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
r = r + 1
Rows(r).Select
Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
r = r + 1
Rows(r).Select
Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
r = r + 1
Rows(r).Select
Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
r = r + 1
Windows("Generated_list.xlsm").Activate
Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
Windows("Feed_list.xlsx").Activate
Sheets("Feed_list").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Generated_list.xlsm").Activate
Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
Windows("Feed_list.xlsx").Activate
Sheets("Feed_list").Select
Selection.Offset(-1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Generated_list.xlsm").Activate
Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
Windows("Feed_list.xlsx").Activate
Sheets("Feed_list").Select
'Rows(r).Select
Selection.Offset(-1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
If cell.Value = "VFD" Then
Debug.Print cell.Address
Windows("Generated_list.xlsm").Activate
Range(cell, cell.Offset(, -25)).Copy
Windows("Feed_list.xlsx").Activate
Sheets("Feed_list").Select
'Debug.Print r
Rows(r).Select
Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
r = r + 1
Rows(r).Select
Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
r = r + 1
Windows("Generated_list.xlsm").Activate
Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
Windows("Feed_list.xlsx").Activate
Sheets("Feed_list").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Generated_list.xlsm").Activate
Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
Windows("Feed_list.xlsx").Activate
Sheets("Feed_list").Select
Selection.Offset(-1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
'these if functions are repeated about 20 times with different text values and number of rows copied
Next
Application.ScreenUpdating = True
Windows("Feed_list.xlsx").Activate
Sheets("Feed_list").Select
End Sub
I made small example pictures. The Generated_list looks like this. (Notice column AB)
The Feed_list looks like this at first.
And after running the script it should look like this.
Sub Main()
Call Add_Rows(8)
End Sub
Sub Add_Rows(whereToAdd As Long)
Dim wb_Feed As Workbook, wb_Gen As Workbook
Dim ws_Feed As Worksheet, ws_Gen As Worksheet
Dim lastRow As Long, lastCol As Long, i As Long, idxType As Long
Set wb_Feed = Workbooks.Open("C:\Projects\Feed_list.xlsx")
Set wb_Gen = Workbooks.Open("C:\Projects\Generated_list.xlsm")
Set ws_Feed = wb_Feed.Worksheets("Feed_List")
Set ws_Gen = wb_Gen.Worksheets("Generated_List")
' Find the last row and last column of the data in Generated List
' Assume that the first column does not contain any blank data in middle
lastRow = ws_Gen.Cells(ws_Gen.Rows.Count, "A").End(xlUp).Row
lastCol = ws_Gen.Cells(1, ws_Gen.Columns.Count).End(xlToLeft).Column ' First row is header
' Column AB is the last column
idxType = lastCol
With ws_Gen
For i = 2 To lastRow
If .Cells(i, idxType).Value = "VFD" Then
' Insert a row to Feed List
ws_Feed.Range("A" & whereToAdd).EntireRow.Insert
' Copy entire row
.Range(.Cells(i, 1), .Cells(i, lastCol)).Copy
' Paste
ws_Feed.Range("A" & whereToAdd).PasteSpecial xlPasteAll
Application.CutCopyMode = False
' Since VFD, insert extra 1 line according to your screenshot
whereToAdd = whereToAdd + 1
ws_Feed.Range("A" & whereToAdd).EntireRow.Insert
' Copy first 5 columns
.Range(.Cells(i, 1), .Cells(i, 5)).Copy
' Paste
ws_Feed.Range("A" & whereToAdd).PasteSpecial xlPasteAll
Application.CutCopyMode = False
' Update where to add next
whereToAdd = whereToAdd + 1
ElseIf .Cells(i, idxType).Value = "DOL-1" Then
' Insert a row to Feed List
ws_Feed.Range("A" & whereToAdd).EntireRow.Insert
' Copy entire row
.Range(.Cells(i, 1), .Cells(i, lastCol)).Copy
' Paste
ws_Feed.Range("A" & whereToAdd).PasteSpecial xlPasteAll
Application.CutCopyMode = False
' Since DOL-1 insert extra 3 lines according to your screenshot
whereToAdd = whereToAdd + 1
ws_Feed.Range("A" & whereToAdd).EntireRow.Insert
ws_Feed.Range("A" & whereToAdd).EntireRow.Insert
ws_Feed.Range("A" & whereToAdd).EntireRow.Insert
' Copy first 5 columns
.Range(.Cells(i, 1), .Cells(i, 5)).Copy
ws_Feed.Range("A" & whereToAdd).PasteSpecial xlPasteAll
ws_Feed.Range("A" & whereToAdd + 1).PasteSpecial xlPasteAll
ws_Feed.Range("A" & whereToAdd + 2).PasteSpecial xlPasteAll
Application.CutCopyMode = False
' Update where to add next
whereToAdd = whereToAdd + 3
End If
Next i
End With
' You should close the workbook after you finish your job
End Sub

How to add the most top & bottom row on VBA

I want to merge the first and the last row using the =cell1&cell2 function of the table but was unable to as the number of row can be dynamic.
Tried using the relative distance using ctrl+up but to no avail.
Ideally a VBA code where I can use the "&" function to merge the most top and last row of the table then paste special on top as text
Sub Macro9()
ActiveCell.FormulaR1C1 = "=R[-9]C&R[-2]C"
ActiveCell.Select
Selection.Copy
Selection.End(xlUp).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(2, 0).Range("A1").Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
If I understand correctly , what you want is a way to address the last cell in a column.
You can do this as follows:
Set sht = Sheets("main")
column = 1
lastRow = sht.Cells(sht.Rows.Count, column).End(xlUp).Row
Set lastCell = sht.Cells(lastRow, column)
lastCell is a range variable referencing the last cell in the column specified by the column variable. I explicitly referenced the sheet to avoid problems with active sheets.
Sub MergeCells()
col = 1 // column A
lastRow = Cells(Rows.Count, 1).End(xlUp).row
Mcell = Cells(1, col) & Cells(lastRow, col)
End Sub

VBA Offset and Paste

I have some VBA code that works fine, however I'm trying to improve my code by losing the select commands. I am learning that this is not best practice. The (old) code that works is below:
With Sheets("Data")
RowCount = .Cells(.Rows.Count, "B").End(xlUp).Row
For i = 1 To RowCount
Range("B1").Offset(1, 0).Select
If ActiveCell.Offset(0, -1).Value = 2 And ActiveCell.Value = sPeril Then
ActiveSheet.Cells.Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("DynamicCharts").Select
Sheets("DynamicCharts").Range("E" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
End If
next i
End With
The code switches between sheets copying and pasting using offset cells. Ive tried to change this with a WITH command and its debugging on the paste command.
With Sheets("Data")
RowCount = .Cells(.Rows.Count, "B").End(xlUp).Row
For i = 1 To RowCount
Range("B1").Offset(1, 0).Select
If ActiveCell.Offset(0, -1).Value = 1 And ActiveCell.Value = sPeril Then
ActiveSheet.Cells.Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
With Sheets("DynamicCharts")
.Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
'Sheets("EDM Data").Select
End If
next i
End With
Any help on this would be much appreicated.
Thanks in advance
Give this a shot instead - this completely removes the need for Select. We could also get rid of Copy/Paste as well, but I need to know what you're trying to bring over (maybe it's format specific?). Please include more of your code when asking a question (like what sPeril is, etc.):
Dim destrow As Long, lastcol As Long
With Sheets("Data")
RowCount = .Cells(.Rows.Count, "B").End(xlUp).Row
For i = 2 To RowCount
If Range("B" & i).Offset(0, -1).Value = 2 And Range("B" & i).Value = sPeril Then
destrow = Sheets("DynamicCharts").Cells(Sheets("DynamicCharts").Rows.Count, "E").End(xlUp).Row
lastcol = Sheets("Data").Cells(i, Sheets("Data").Columns.Count).End(xlToLeft).Column
Sheets("Data").Range(Sheets("Data").Cells(i, 2), Sheets("Data").Cells(i, lastcol)).Copy
Sheets("DynamicCharts").Range("E" & destrow + 1).PasteSpecial
End If
Next i
End With
your code but with a simple fix just look at the comment. Note I set peril to 2 just so that i can make the code fall into that condition.
Sub test2()
With Sheets("sheet1")
RowCount = .Cells(.Rows.Count, "B").End(xlUp).Row
For i = 1 To RowCount
Range("B1").Offset(1, 0).Select
sPeril = 2
If ActiveCell.Offset(0, -1).Value = 1 And ActiveCell.Value = sPeril Then
ActiveSheet.Cells.Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
With Sheets("DynamicCharts")
'remove selection on this line.
.Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
'Sheets("EDM Data").Select
End If
Next i
End With
End Sub

How to enter data from a form on one sheet onto a log on another sheet

So I'm trying to figure out how I can setup a macro that will take the data that I enter into a form on one sheet then log it into a log in another sheet. It will log it but my big problem is that it needs to go to the next line and I can't quite figure out the code for it. Here is what my code looks like:
Sub Appt()
'
' Appt Macro
'
'
Range("E4").Select
Selection.Copy
Sheets("Appointments").Select
Range("G7").Select
ActiveSheet.Paste
Sheets("Data Entry").Select
Range("E6").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Appointments").Select
Range("D7").Select
ActiveSheet.Paste
Sheets("Data Entry").Select
Range("E8").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Appointments").Select
Range("E7").Select
ActiveSheet.Paste
Sheets("Data Entry").Select
Range("E10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Appointments").Select
Range("F7").Select
ActiveSheet.Paste
Sheets("Data Entry").Select
Range("E12").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Appointments").Select
Range("H7").Select
ActiveSheet.Paste
Sheets("Data Entry").Select
Range("E4").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("E6").Select
Selection.ClearContents
Range("E8").Select
Selection.ClearContents
Range("E10").Select
Selection.ClearContents
Range("E12").Select
Selection.ClearContents
End Sub
To get the next empty row on Sheets("Appointments") you would use this formula to get the row number:
tRw = Sheets("Appointments").Range("D" & Rows.count).End(xlUp).Offset(1).Row
This assumes that there is nothing in column D below what you are pasting.
It is apparent that you used the macro recorder, and this is a great way to learn. But using the .select so much will slow things down and is unneeded.
To get around that declare the sheets as variables and then one line for each copy paste is needed.
Sub APPT()
Dim oWs As Worksheet
Dim tWs As Worksheet
Dim tRw As Long
Set oWs = Sheets("Data Entry")
Set tWs = Sheets("Appointments")
tRw = tWs.Range("D" & Rows.count).End(xlUp).Offset(1).Row
With oWs
.Range("E4").copy tWs.Range("G" & tRw)
.Range("E6").copy tWs.Range("D" & tRw)
.Range("E8").copy tWs.Range("E" & tRw)
.Range("E10").copy tWs.Range("F" & tRw)
.Range("E12").copy tWs.Range("H" & tRw)
.Range("E4").ClearContents
.Range("E6").ClearContents
.Range("E8").ClearContents
.Range("E10").ClearContents
.Range("E12").ClearContents
End With
End Sub
For other methods of finding the next row look at Siddharth Rout's answer here.
And as BruceWayne stated in his comment, this is a great reference as to why/how to avoid using .select
It's best to avoid the user's clipboard and to assign the values directly:
Sub Appt()
Dim n&, v
v = [transpose(offset('data entry'!e4,{0;2;4;6;8},))]
With Sheets("appointments")
n = .Range("d" & .Rows.Count).End(xlUp).Row
.[g1].Offset(n) = v(1)
.[d1].Offset(n) = v(2)
.[e1].Offset(n) = v(3)
.[f1].Offset(n) = v(4)
.[h1].Offset(n) = v(5)
End With
Sheets("data entry").Range("e4,e6,e8,e10,e12").ClearContents
End Sub

Resources