Macro to auto fill text in a column down to the last cell in one table (while there are additional tables below) - vba

example of tables in the spreadsheet
Hi I am new to VBA and wanted to create a macro that can auto fill column "Y/N" (as shown in the attached pic) with the text "Yes" by clicking a button.
Right now I have 3 rows in the first table, which is fine and I used the following code:
Sub autofill1()
Range("E12").Value = "Yes"
Range("E12").Select
Selection.AutoFill Destination:=Range("E12:E14")
End Sub
However, the rows in the first table will keep changing, say it becomes 20, 50, 100 depending on different cases. I wonder if there's a way for me to define the auto fill range to be cell E12 to the last cell in this table, not to extend to the rest of the tables below.
I checked the codes provided for other similar questions - looks like it will auto fill all the way down to the last cell in my worksheet, which is the last cell in this column of the 5th table. And I don't want that. Is there a way to only auto fill down to the last cell of the 1st table? Thanks so much in advance!

In my opinion, adding "yes" until found a bottom border.
Sub autofill1()
'Range("E12").Value = "Yes"
'Range("E12").Select
'Selection.AutoFill Destination:=Range("E12:E14")
Dim i As Long
i = 11
Do While Range("E" & i).Borders(xlEdgeBottom).LineStyle _
= xlNone
i = i + 1
Cells(i, 5) = "Yes"
Loop
End Sub

Related

Selecting a cell and copiying the corresponding cells in the row in VBA

I am trying to build a macro and I want to copy a row after selecting a specific cell with the filter. So I filter a cell in column "A" and then I want to copy all the cells in that row. The problem is that the rownumber corresponding to the selected cell may change, due to different datasets.
Below is the code:
Selection.AutoFilter
ActiveSheet.Range("$A$1:$DO$46").AutoFilter Field:=2, Criteria1:= _
"NAME"
Range("A5:DO5").Select
Selection.Copy
The point is that in VBA the selection is set at "A5:DO5", because in this specific dataset "NAME" is on "A5".
But in a different dataset "NAME" might be on "A9", but in VBA the selection of the row is still on "A5:DO5".
How can I make the selection of "NAME" and the copy of all the cells in the row of "NAME" linked to eachother?
I think the question could be a bit clearer, but here goes.
As I understand it you want to copy a row, say 5th row down, after a filter has been applied to hide some rows in between (5th visible row).
Or, you want to find "NAME" and copy the corresponding row, irrespective of what's hidden and what isn't.
(If I haven't got the above right, please let me know and/or consider clarifying the question)
Try something like this;
Dim C as Cell
Dim RowNum as Integer 'If you're looking for the 5th visible cell.
For each C in Range("A:A").SpecialCells(xltypeVisible)
RowNum = RowNum+1 'Count how many visible cells you've looped
If C.Value = "NAME" Then
'Or for the 5th visible cell, use 'If RowNum = 5 Then'
C.EntireRow.Copy
Exit For
End If
Next
Thank you for your answer. Unlukily, the code is not working.
To clarify:
I want to find "NAME" in Column "B" and copy the corresponding row of "NAME", irrespective of what's hidden and what isn't.
Now I have this code:
Dim C As Range
For Each C In Range("B:B")
If C.Value = "NAME" Then
C.EntireRow.Copy
Exit For
End If
Next
The code does copy a row, but not the row that corresponds with "NAME". Instead, it copies the first row in the active sheet.

VBA: filter data based on cell value and fill in to another sheet

enter image description hereI Have two Excel Sheets ("Record") & ("Register"), " Register" is the database. from this database I need to create Records of each employees based on their employee ID (cell value). i am searching for a VBA code that should give me the training Record a each employee once i have enter their ID in the cell and click "a command button". Attached the Excel screen short for reference.
Steps 1: Enter Employee ID in the "Record" sheet
Step 2: Click command button "Filter" in the Record sheet
Step 3: VBA code to run and filter data from "Register" and fill "Record".
IF i Type another Employee ID in the sheet "Record" , it should ClearContents of the previous query. and produce the data.
Please help me, i am not good in VBA .attached the Excel screen short for reference ( UPDATE on 29/07/2018-Question Solved : sharing the code below; thank you Mr.ComradeMicha for your valuable feedback)
Sub Button2_Click()
'Declare the variables
Dim RegisterSh As Worksheet
Dim RecordSh As Worksheet
Dim EmployeeRange As Range
Dim rCell As Range
Dim i As Long
'Set the variables
Set RegisterSh = ThisWorkbook.Sheets("Register")
Set RecordSh = ThisWorkbook.Sheets("Record")
'Clear old data Record Sheet
RecordSh.Range("A8:F107").ClearContents
Set EmployeeRange = RegisterSh.Range(RegisterSh.Cells(6, 4), RegisterSh.Cells(Rows.Count, 6).End(xlUp))
'I went from the cell row6/column4 (or D6) and go down until the last non empty cell
i = 7
For Each rCell In EmployeeRange 'loop through each cell in the range
If rCell = RecordSh.Cells(4, 2) Then 'check if the cell is equal to "Record"
i = i + 1 'Row number (+1 everytime I found another "Record")
RecordSh.Cells(i, 1) = i - 7 'S No.
RecordSh.Cells(i, 2) = rCell.Offset(0, 2) 'Training name
RecordSh.Cells(i, 3) = rCell.Offset(0, -2) 'End date
RecordSh.Cells(i, 4) = rCell.Offset(0, 6) 'Validity
RecordSh.Cells(i, 5) = rCell.Offset(0, 7) 'Remarks
RecordSh.Cells(i, 6) = rCell.Offset(0, 5) 'Minimal requirement
End If
Next rCell
End Sub
Your code is missing a few essential parts you may want to look into:
It seems to require the user to select a specific row before the macro is started, even though there is a command button to trigger the macro. If the layout stays the same, just use constants to store the row where certain input or lookup fields are located.
ra is used both on the input form and on the lookup sheet. That's asking for trouble... Again, use constants or at least "StartingRow=3" or something similar.
You are correcting your employee number to a format that doesn't fit the data provided in the screenshot. Hopefully just a dummy data issue, but in case you are wondering why you don't find anything ;)
You are typecasting all fields individually. If your layout is always the same, it's much easier to just use the "variant" type for all cell values and make sure you already formatted all columns correctly.
ru is never initialized? It's supposed to be "the next row", why not simply use "ra+1" then instead of ru? Also, TRNRow and RTRNRow are never initialized either.
When you "search" your records, you actually only copy the same row into your results, then "copy next row until employee number is wrong". So this only works for exactly one employee, and even then you only catch the first few trainings. Use the Find function on the employee number cell in the records sheet to find the next row with that id, then copy the row's contents and find next.
I think if you get yourself aquainted with the Find function, you will easily finish this macro on your own. Here's a good guide: https://excelmacromastery.com/excel-vba-find
Good luck!

Copy rows based on cell value and paste on a new sheet

Check This
I need a help. I want to copy whole cell from A sheet name "Components" only if value in Column C is > 0 to a new Sheet name "Load list"
Can someone please give me the macro code for this?
on your new sheet you can add this condition the cell or range of cells:
=IF(Components!C5>0,Components!A5)
where C5 has thevalue to compare, and A5 has the value copy if the condition happens.
Right in my swing!
The formula given by #sweetkaos will work fine, in case you want to replicate the data as it is with blanks where data is not found.
I will imagine a slightly more complicated situation. I am assuming you want just one line in the next format as is shown in your image.
Also conveniently assuming the following:
a. both sheets have fixed start points for the lists
b. 2 column lists - to be copied and pasted, with second column having value
c. Continuous, without break source list
d. basic knowledge of vba, so you can restructure the code
Here is the code. Do try to understand it line by line. Happy Excelling!
Sub populateLoadList()
'declaring range type variables
Dim rngStartFirstList As Range, rngStartLoadList As Range
'setting values to the range variables
'you must change the names of the sheets and A1 to the correct starts of your two lists
Set rngStartFirstList = Worksheets("Name_of_your_fist_sheet").Range("A1")
Set rngStartLoadList = Worksheets("Name_of_your_second_sheet").Range("A1")
Do While rngStartFirstList.Value <> ""
If rngStartFirstList.Offset(1, 0).Value < 0 Then
Range(rngStartFirstList, rngStartFirstList.Offset(0, 1)).Copy
rngStartLoadList.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Set rngStartLoadList = rngStartLoadList.Offset(1, 0)
End If
Set rngStartFirstList = rngStartFirstList.Offset(1, 0)
Loop
End Sub
Basically what i want is ... if Value on C is >0 i want whole column 10 copied to that new sheet .... not only that cell

Excel VBA deleting certain rows with certain conditions

I found a code online which works but I am failing to change it for my purpose. Each entry in my spreadsheet contains different formulas as well as an Iferror function with the aim of making cells with error messages appear as blank. For example lets say a cell E3 is dependent on cell F3 with a certain formula (for clarification lets say F3/2.5). It is obvious if there is no entry in cell F3 then an error message would display in cell E3. For this reason, I use the IFERROR function to display the cell as blank. The difficulty arises when I want to delete blank rows after a click on the macro button. However, since that cell does have an entry (a formula which in turn returns an error message), that cell does not delete. Also I need to run this code over 3 different selection ranges. Please can someone help! The code I found was from a different thread on this forum and is:
`sub foo()
dim r As Range, rows As Long, i As Long
Set r = ActiveSheet.Range("A1:Z50")
rows = r.rows.Count
For i = rows To 1 Step (-1)
If WorksheetFunction.CountA(r.rows(i)) = 0 Then r.rows(i).Delete
Next
End Sub`
Thanks Alot!
EDIT: If statement added to the autofilter as it was deleting a row when there were no blanks
You will want to set up a column in the spreadsheet with the following sumproduct:
=SUMPRODUCT((LEN(A1:F1)>0)*1)
This is calculating how many cells' values have a length more than 0 hence are not blank, you will need to adjust cell references accordingly as I tested on a small sample of fake data.
Following this you can just loop:
For i = rows To 1 Step (-1)
If Cells(i,"G") = 0 Then r.rows(i).Delete 'My formula is in column "G"
Next
Or set up an auto-filter and delete entire rows of the visible cells:
Dim lrow As Integer
If Not WorksheetFunction.CountIf(Range("G:G"), "0") = 0 Then
Range("A1:G1").AutoFilter
Range("A1:G1").AutoFilter Field:=7, Criteria1:="0"
lrow = Cells(rows.Count, 7).End(xlUp).Row + 1
Range("G2:G" & lrow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
Range("A1:G1").AutoFilter
End If
The only problem with using a leading column to calculate for this is if you have a lot of data coming and going as you will need to replenish the formula, though you could use auto complete in the code i guess.

Excel, conditional formatting column base on comparing two columns

I have excel worksheet, with two sheets. First one (Worksheet 1) is big table (about 2000 rows), and in second (Worksheet2) I have only about 20 rows.
I need to check, if string in first column in each row in worksheet 1 is already in Worksheet 2, if so, make it green.
Thanks for solutions, both EXCEL or VBA will be appreciated :)
In worksheet 1 you need to create a new column (say column AA) which uses the formula VLOOKUP to find the same data (if it exists) in the other sheet. If is is not found VLOOKUP will return and error #N/A. So you formula will look soemthing like:
IF(ISERROR(VLOOKUP(A2, 'Sheet2'!$SA$1:$X$9999, 3, false)),"NOT FOUND","FOUND")
You would then add conditional formatting to sheet1 column A that references the value in your new column (AA) and sets the colour accordingly.
If you don't know how to use VLOOKUP or get the conditional formatting to work, there are plenty of resource on the internet to help you learn these. eg youtube channel ExcelisFun, WiseOwl.co.uk.
I hope this helps.
PS You do not need to use VBA to do this! In fact using a foumula can be preferable as if a single value changes it can cause the colours to be changed.
Harvey
If you want to use VBA, try as follow:
Public Sub findDuplicate()
Dim sh1row, sh2Row As Long
For sh1row = 1 To 20 'loop all row from Sheet2 (if more than 20, modify it)
For sh2Row = 1 To 2000 'loop all row from Sheet1 (if more than 2000, modify it)
'If A cell are equal
If Sheets("Sheet1").Range("A" & sh2Row) = Sheets("Sheet2").Range("A" & sh1row) Then
'Change background color to green.
Sheets("Sheet1").Range("A" & sh2Row).Interior.Color = RGB(0, 255, 0)
End If
Next sh2Row
Next sh1row
End Sub