I am using Excel 2013, trying to insert a new column at the start of my sheet (Column A) & then insert text into the newly created cells. I would like to insert the word "OR" into every new cell created from row 1 all the way down to the last row that contains data within my sheet. The amount of rows in the sheet will change daily so ideally I would like the code to auto detect the amount of rows if possible rather than the code having to be changed daily.
I have searched for the answer on here & can find how to insert a new column or text but I can't find the specific code required for my above issue.
Any help would greatly be appreciated please
How about the following, it will count the number of rows in Column A, then insert a new Column and enter "OR" on each cell from Row 2 to Last:
Sub foo()
Dim rng As Range
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
ws.Columns("A:A").Insert Shift:=xlToRight
'insert new column A
Set rng = ws.Range("A2:A" & LastRow)
'set range from A2 to Last, amend if your data has no headers and you want to insert "OR" into A1 too.
rng.Value = "OR"
'add the value "OR" to each cell
End Sub
Related
I am brand new to VBA as a disclaimer.
I have data I am inputting into one sheet (Emptys) and am trying to grab a cell from another sheet (report) in the same row that has a blank in a certain column.
For example:
Sample Report
I am trying to pull the value from the column A if the cell in the same row of column M is empty and reference that to a different sheet.
I am also trying to skip any rows that have a value in them and only pull the data from the rows that have a blank in column M.
I have tried a few things and I am in a bit over my head.
All that I have gotten to work is this basic formula:
=IF( Report!M2= "",Report!A2, "" )
I still have to sort out empties manually this way.
I feel like I was on the right track here but not sure where I went wrong:
Dim myrange
Dim id
myrange = Sheets("Report").Range("M2:M")
id = Sheets("Empty_Slots").Range("A2:A")
For Each cell In myrange
If IsEmpty(cell) Then
id = Sheets("Report").Range("A2:A")
End If
Next cell
Any help and explanation would be greatly appreciated!
You may access row numbers of those blank cells and use them in worksheet 2:
Sub Macro1()
Dim wk1 As Worksheet
Dim wk2 As Worksheet
Dim LR As Long
Dim rng As Range
Set wk1 = Worksheets("Sheet1")
Set wk2 = Worksheets("Sheet2")
With wk1
LR = .Range("A" & .Rows.Count).End(xlUp).Row 'last non blank cell in column a
For Each rng In .Range("M2:M" & LR).SpecialCells(xlCellTypeBlanks) 'array of blank cells in column M
wk2.Range("A" & rng.Row).Interior.Color = vbYellow 'do something with column A from worksheet2 based on row numbers from column M in worksheet 1
Next rng
End With
Set wk1 = Nothing
Set wk2 = Nothing
End Sub
The code loops trough each blank cell in column M from Sheet1 and pulls the row number. Then in worksheet 2 it uses that row number in column A.
After executing code, I've colored cells in sheet2 but based on sheet1
I'm currently exporting a document (wsCopy) from a software and paste special valuing the data to another excel workbook I'm creating (wsDest). Within (wsDest), I also have two vlookups, columns AI:AJ, which reference the newly created exported data, a "SupportReference" tab, and a "RegionLookup" tab. My issue is that the values in the exported data is formatted weirdly. Even though the exported data format says "General", the vlookups in column AJ are returning #N/A errors. The only ways to fix this problem is if I click on the referenced cells (column AH), which includes the exported data, hit F2, then Enter, or create a vba that will multiply all of the vlookups in column AI by 1. However, I don't want to do the first option since I'll have to do it for ~14000 rows. The issue with the second option is that, multiplying the entire AI column by 1 won't work if the vlookup in column AI returns with any letter; also, it gets rid of the vlookup in column AI. Below is what I came up with:
Sub CopyOver()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Set wsCopy = Workbooks("Export").Worksheets("Sheet1")
Set wsDest = ThisWorkbook.Worksheets("Data")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
'3. Clear contents of existing data range
wsDest.Range("A9:AH" & lDestLastRow).ClearContents
'4. Copy & Paste Data
wsCopy.Range("A2:AH" & lCopyLastRow).SpecialCells(xlCellTypeVisible).Copy
wsDest.Range("A9").PasteSpecial Paste:=xlPasteValues
'Start copy down formulas
Dim DestLastPopulatedRow As Long
'Find last populated row
DestLastPopulatedRow = wsDest.Range("A" & Rows.Count).End(xlUp).Row
'Select the rows where formula is to be populated
wsDest.Range("AI9: " & "AJ" & DestLastPopulatedRow).FillDown
End Sub
The very last part of the code (wsDest.Range("AI9: " & "AJ" & DestLastPopulatedRow).FillDown) is where I start having issues because the vlookup formula starts in cell AI9. The vlookup formula is as follows:
Column AI Vlookup:
=VLOOKUP(AH9,'SupportReference'!E:E,1,FALSE)
Column AJ Vlookup:
=VLOOKUP(AI9,'RegionLookup'!M:M,1,FALSE)
Attempted VBA code multiplying column AI:
With wsDest.Range("AI9: " & "AI" & DestLastPopulatedRow)
.Value = Evaluate(.Address & "*1")
End With
Let me know if you need more clarification or further data.
Thank you.
If you don't need the VLOOKUP functions to be active, which I expect to be the case because you are copying a large block of data in, you can use VBA to set the value of the lookup cells instead of a function. Here's a sub-procedure where you pass it a destination cell and other lookup parameters, and the VBA finds the value and puts it in the destination cell:
Sub exact_lookup(destination As Range, lookup_value As Variant, lookup_table As Range, column As Integer)
Dim foundCell As Range
Set foundCell = lookup_table.Columns(1).Find(lookup_value, , , xlWhole)
If foundCell Is Nothing Then
destination.Value = CVErr(xlErrNA)
Else
destination.Value = foundCell.Offset(0, column - 1).Value
End If
End Sub
Then write a loop to fill the value of each cell where your current approach uses lookup formulas. An example call is:
exact_lookup wsDest.range("AI9"), wsDest.range("AH9").value, worksheets("RegionLookup").range("E:E"), 1
Note: I'm not entirely sure I understand just what you doing, but hopefully this approach will work this is enough to get headed in the right direction.
I have a table with columns A-C, C might have 20 or 200 cells, it changes so it can't be fixed, I want to have a macro that creates a Column D starting at D2, fills up all the way down to the end of column C, so if C has a total of 50 rows, D will have 49 but ending at D50 since it starts at D2.
So, how could I do this as an excel macro with VBA or manually? Thanks!
You can either do it manually by writing the value and double-click in the cell corner to fill in values. It requires that the adjacent cell have values!
Or you can use a basic VBA code that do it for you. It check the last row in column C and fills column D.
Option Explicit
Sub Fill_in_values()
Dim ws As Worksheet
Dim lrow As Long
Set ws = ActiveWorkbook.Worksheets("Sheet1") 'Set worksheet name
lrow = ws.Cells(Rows.Count, "C").End(xlUp).Row 'Find last row in Column C in Sheet1
ws.Range("D2", "D" & lrow).Value = "0.8" 'Fill from D2 to last row.
End Sub
I currently have multiple sheets for storing records of payments (things to be Pay and CantPay). I am trying to write a macro that will copy and paste Cells A:M on every row where column T = "Resolved" on the CantPay sheet (where the next empty row is the next row where "a" & row-number = blank) to the "Pay" sheet.
Within the sheet which i want to copy from there is data in columns A:T but N:T are not needed once the problem is resolved. So once i have copy and pasted the data within cells A:M i want to just delete the entire row. I have written some code from what i knew and looking online which isn't working. Any help would be much appreciated.
Thanks
I have tried recording a macro and writing my own but it seems the macro i have wrote is deleting row 1 which is where all my column headers are stored.
Sub MoveToPay()
Dim CantPay As Worksheet: Set CopySheet = Sheets("Can't Pay")
Dim ReadyToPay As Worksheet: Set PasteSheet = Sheets("£ Pay")
Dim lr As Long
Dim S As String
Application.ScreenUpdating = False
Columns(20).AutoFilter 1, "Resolved"
With Range("a2", Range("M" & Rows.Count).End(3))
.Copy PasteSheet.Cells(Rows.Count, 1).End(3).Offset(1)
.EntireRow.Delete
End With
Columns(20).AutoFilter
Application.ScreenUpdating = True
End Sub
This is my first question on SO and I'd really appreciate the direct help:
I have an Excel file with multiple worksheets. In Column D of each worksheet, I've created insert SQL statements based on each row's dynamic data. I'd now like to copy all of the insert statements that I've created in Column D on each worksheet into one big column so I can run them all into my database.
Thanks SO much for the help :)
To implement:
Create new sheet titled Master
Give cell A1 a header name
Open VBE
Insert Module
Copy and paste code as is into module
The code will:
Loop through each sheet
Copy cells in Col D from D2 To Last Non-Blank Row
Paste values only on Master in next available Non-Blank Row in Col A
Loop 2 - 3 for all sheets
Option Explicit
Sub MyDearMacro()
Dim ws As Worksheet, Master As Worksheet
Set Master = ThisWorkbook.Sheets("Master")
Dim CopyRange As Range, PasteRange As Range
Application.ScreenUpdating = False
For Each ws In Worksheets
If ws.Name <> "Master" Then
Set CopyRange = ws.Range("D2:D" & ws.Range("D" & ws.Rows.Count).End(xlUp).Row)
Set PasteRange = Master.Range("A" & Master.Rows.Count).End(xlUp).Offset(1)
CopyRange.Copy: PasteRange.PasteSpecial xlPasteValues
End If
Next ws
Application.ScreenUpdating = True
MsgBox "Please take the tour page of Stack OverFlow and show an attempt to solve on your next post.", vbCritical
End Sub
Closing Notes:
You will need to amend this if you do not want duplicates (if they exist). You may also need to amend this if your SQL Statements do not cover every row in the copy range (you may end up with blank cells separating your Statements). AKA once loop is done, remove blanks and duplicates in Col A on Master sheet.