How to select a row data before blank cell? - excel

For i = 3 To 50
If lngRow = Range("A" & i) Then
Range("A1:EN3").Rows(i).Copy
Range("A1:EN3").Columns(strCol).Offset(, 1).PasteSpecial Transpose:=True
Range("A1:EN3").Rows(1).Copy
Range("A1:EN3").Columns(strCol).PasteSpecial Transpose:=True
Exit For
End If
Next i
Hello I have written code for selecting the row and paste it into column wise
it is working correctly but my problem is here i am giving range as Range("a1:en3") but every time it may be more the values than this so is it possible to copy the row data before blank cell like how we can copy the column before blank cell i,e Range(rng, rng.End(xlDown)).Copy.

Function firstblank()
i=1
While (worksheets("WORKSHEETNAMEHERE").cells(i,1).value<>"")
i=i+1
wend
return i
End function
Not totaly sure I understand your question but pasting the transposed values should be as easy as:
Range("T5").Select
Selection.PasteSpecial Transpose:=True
(i would not define a range bigger than one cell to write the values; if you give more than one cell excel will require your target to have the exact dimensions of what you are trying to write)

Related

Is there a way to format exports from azure differently in excel?

I just started working with large quantities of data from Azure and other software products that produce CSVs or excel files that produce information formatted as follows:
Is there an easy way to format it like:
So that it can be used effectively as a table?
Thank you!
Here's an algorithmic approach that you can implement in VBA
Get references to the Sheet and Range containig the data
Copy the Data to a Variant Array variable
Loop throught the Data Array rows
For each row that column A is Not Empty, capture the value
For each row where Column B is Empty, clear Column A
For each row where Column B is Not Empty, write the captured value to Column A
After the loop copy the Data Array back to the sheet
This works for me.
Sub TryMe()
' fill down from above
Range("A1:A20").Select ' change the range to suit your needs
Range("A20").Activate
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"
' copy/paste values; no references to other cells
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A1").Select
' use come logic to do cleanup
Dim rng As Range, cell As Range
Set rng = Range("B1:B20") ' change the range to suit your needs
For Each cell In rng
If cell.Value = "" Then
cell.Offset(0, -1).Value = ""
End If
Next cell
End Sub
Before:
After:

vba multiple checkboxes

I'm fairly new to VBA and i'm struggling to figure out code to copy and paste certain values to a different worksheet
I've got some code that does work, however it is very slow to run:
If CheckBox1.Value = True Then Sheets("Sheet1").Range("A2:B2").Copy Destination:=Sheets("Sheet2").Range("A2:B2")
If CheckBox2.Value = True Then Sheets("Sheet1").Range("A3:B3").Copy Destination:=Sheets("Sheet2").Range("A3:B3")
If CheckBox3.Value = True Then Sheets("Sheet1").Range("A4:B4").Copy Destination:=Sheets("Sheet2").Range("A4:B4")
...............
If CheckBox53.Value = True Then Sheets("Sheet1").Range("A54:B54").Copy Destination:=Sheets("Sheet2").Range("A54:B54")
I've got each checkbox linked to the cell next to it, e.g E2, E3, E4 etc to display True/False depending on whether is been clicked or not. Also
I'm thinking that a for loop would be better,e.g for "a=2 to 53" and use the true/false values in column E, however i'm unsure how to modify the range of cells within a for loop so that it increases just like "a", so A2:B2, A3:B3 etc
Also, the other problem with this code is that is copies the values to the same cells within the 2nd sheet. What would be the best way to get it to paste the values to the next available row.
If anyone could point out somewhere there's some similar example code that would be extremely useful so that i can learn how to do it myself.
Kind regards
Chris
edit with working code
This is the code that i've come up with to fulfill the criteria outlined above
Sub checkbox()
For i = 2 To 53
Sheets("Sheet1").Select 'activates sheet 1
If Sheets("sheet1").Cells(i, "E").Value = True Then ' checks for true value
Range("A" & i & ":B" & i).Select 'selects range of cells for copying
Selection.Copy
Sheets("Sheet2").Select 'selects sheet 2
Range("A65536").End(xlUp).Offset(1, 0).Select 'selects next available empty row
ActiveSheet.Paste 'pastes value
Else
End If
Next i
End Sub

How to paste to the last filled row without including first row (headers)?

I'm trying to paste data at the end of the last filled row in a sheet, but my syntax overwrites my headers in Row 1 during the first iteration of the loop.
I'm running a looped code that copies and transposes variables from multiple spreadsheets on a single sheet. Variables are in two columns in the source files but go onto one row in the destination file. I use a "next row" function to place the data from the first column into the first empty row. I then want to use a "last row" function to append the data from the second column to the same row. However, during the first operation, my code doesn't know the difference between the first line of data and the variable names on the first row, so the second column data ends up shifted one row up from the first half of the data.
Dim lastrow As Long
Dim nextrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
nextrow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row + 1
'assigns objects for the last filled row and the next unfilled row
wb1.Worksheets("Database").Range("B1:B578").Copy
Workbooks("zzmaster.xlsx").Worksheets("Sheet1").Cells(nextrow, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
DoEvents
Application.CutCopyMode = False
'copies the relevant data from Column B in Sheet 3 of the source file...
'then transposes and pastes into the next available row of the destination file
wb1.Worksheets("Database").Range("C32:C578").Copy
Workbooks("zzmaster.xlsx").Worksheets("Sheet1").Cells(lastrow, 579).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
DoEvents
Application.CutCopyMode = False
'copies the relevant data from Column C in Sheet 3 of the source file...
'then tranposes and pastes into the same row as the previous function, beginning with...
'the next cell after the last data point
Is there a good way to adjust the syntax so that data does not paste to the first row of the spreadsheet? The goal is to have both columns from a source spreadsheet be pasted to the same row, and that each spreadsheet in the loop be pasted to the next empty row available. I appreciate any suggestions.
I think you want to change the cell you are pasting to, you don't want to paste in lastrow it should be nextrow.
You have;
Workbooks("zzmaster.xlsx").Worksheets("Sheet1").Cells(lastrow, 579).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Change to;
Workbooks("zzmaster.xlsx").Worksheets("Sheet1").Cells(nextrow, 579).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Looks like it wouldn't only be the first iteration of your loop but all of them. Try adding a breakpoint where you set lastrow and step through the execution to see where the issues arises, it will help to step through a few iterations of your loop and assess if it only happens once or not.
Assuming your header is at row 1, can you add coding after this:
lastrow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
If lastrow=1 then lastrow=2

how to value to integer VBA

I’m trying to create VBA that allows me to copy the one of a cell and paste over until end of the row in the same column.
But I’m unable to figure out how this works, I have a total of 109 rows with 20 columns and in column “BD” that is an empty column and I want to put today date and fill up rest of the same column until end of row.
Please see the code at the bottom
Sub CopyInsert()
Range("BD2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("BD2").Select
Selection.Copy
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
ActiveSheet.Paste
End Sub
This code is working fine but I’m not getting the right result what these codes do is copy and paste over pass 109 rows mean is paste over up to around 2000 rows
My question is how can I copy and paste over until the end of the row like till row 109! If we have over 200 rows, how to copy and paste until end of the row, row 200 in one column! But not using (BD2:BD), I have tried (BD2:BD) and is paste over 109 rows is not stopped at the end of row.
many thanks.
So you need to find the lastRow based on the column where the actual data is...
Try something like this:
With worksheets("mySheet")
Dim lastRow as Long
lastRow = .Cells(.Rows.Count,1).End(xlUp).Row 'this uses column A for example
.Range("BD2:BD" & lastRow).Formula = "=TODAY()"
End With

VBA, empty cell is at last row

In a VBA Excel macro,
I copy rows from another sheet, select a different sheet, and try to find the next open cell before pasting them.
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
The page has a first row of column titles, and find then passes over these to the very last row???
If dummy data
is put in the second row, or if there is data already present,
it works.
WHY is it doing this?
Range.End(xlDown) is equivalent to pressing Ctrl + ↓.
Test it on your own - if you only have a header in Row 1, then you'll jump to the last row in the Worksheet. If you have data below the header, you'll move to that row.
Use Range.End(xlUp) to find the next available row by moving up from the last row on the Worksheet - something like this:
With Sheet1
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With
standing your data layout, you can use:
Cells(WorksheetFunction.CountA(Columns(1)) + 1, 1).PasteSpecial Paste:=xlPasteValues
or, using explicit worksheet references (recommended):
With Worksheets("mySheetName")
.Cells(WorksheetFunction.CountA(.Columns(1)) + 1, 1).PasteSpecial Paste:=xlPasteValues
End With

Resources