I've got an Excel file with N rows and M columns. Usually data are organized one per row, but it can happens that a data occupy more than a row. In this case how can I express that the second (or next) row has to refer to the first row?
In this example, AP.01 has got 5 rows of description, so how can I say that the other 4 rows refer also to the first code?
EDIT once that I did the association I have to export my Excel file into an Access DB. So I want to see the tables with the correct data.
If I have only one row for the description I wrote this code and it works:
If grid(r, 3).Text.Length > 255 Then
code.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
code.Description = grid(r, 3).Text.ToString
End If
Instead if I have more than one row for the description I wrote this code and it doesn't work:
Do While grid(r, 1).ToString = ""
If grid(r, 1).ToString = "" And grid(r, 3).ToString IsNot Nothing Then
Dim s As String
s = grid(r, 3).ToString
code.Description = grid((r - 1), 3).ToString & s
End If
Loop
If it is a one-off, try the below. This will basically put a formula in every cell that refers to the cell immediately above it:
Select column A (from top until bottom of list (row N)
Press ctrl + g to open the GoTo dialogue
Press Special
Select Blanks from the radio buttons
The above will select all the blank cells in column A. Now enter = and press up arrow. Enter the formula by holding down ctrl while pressing enter. That will enter the same formula in every cell.
Try
Sub Demo()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet3") 'change Sheet3 to your data sheet
With .Range("A:A").SpecialCells(xlCellTypeBlanks)
.FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
End Sub
From your question I Guess that, you must be define a variable for last column Value. and check the value in respective column, if it is empty then use column value if not empty then take current value as last value.
'Dim LastValue as string
LastValue = sheet("SheetName").cells(i,"Column Name").value
for i = 2 to LastRow '>>>> here i am assume you run code in for loop from row to
'to last count row(LastRow as variable)
'Put your sheet name at "SheetName" and column index (like "A","B","C"...) at "Column Name"
if sheet("SheetName").cells(i,"Column Name").value <>"" then
LastValue = sheet("SheetName").cells(i,"Column Name").value
end if
'(Do your stuff using LastValue , you may generate lastvalue 1, lastvalue2 ..etc)
next'for loop end here
Related
I am trying to bold the last line of a multi-line cell (column E), but specifically based on if another cell in the same row (column L) is blank/empty or not. I have working code that bolds just the last line, but trying to incorporate the IF portion has me stuck. This is what I have so far, and it keeps giving me a data mismatch error.
Thanks in advance for any help or advice.
Sub BoldLastLine1()
Dim p As Long
Dim r As Range
For Each r In ActiveSheet.Range("A3:L100")
If Len(Trim(ActiveSheet.Cells(r, 12).Value)) <> 0 Then
p = InStrRev(r.Value, vbLf)
If p > 0 Then
With r.Characters(p + 1, Len(r.Value) - p).Font
.Bold = True
.Size = 16
End With
End If
End If
Next
MsgBox ("Updates Completed.")
End Sub
The cells-property of a worksheet or range expects 2 numeric parameter for row and column.
You are defining a Range r and passing it as first parameter, this causes the error.
You can use for example
If Len(Trim(ActiveSheet.Cells(r.Row, 12).Value)) Then
This points to the cell in column 12 (=L) of the row of range r
UPDATE:
Cells accepts also a string as second parameter, you could also write
If Len(Trim(ActiveSheet.Cells(r.Row, "L").Value)) Then
I have an extremely large dataset with customer numbers and we cannot just use a =IF(E3=160248, "YES", "NO") to tag a particular customer number of 160248 with YES or NO. Instead, I would like to use VBA code to lookup Customer_Number in column E and return a YES or NO in the corresponding row in Column AG, called Incorporated_160248. I have not done an If then clause in VBA, so I have no idea where to start. Please note, each month the data set can change. One month it could be 4,000 entries and the next 3,500, so that has to be dynamic. Any thoughts?
Sub TagTryco()
Dim CN As Integer, result As String
CN = Range("E:E").Value
If CN = 160248 Then
result = "YES"
Else
result = "NO"
End If
Range("AG:AG").Value = result
End Sub
I get a Compile error: Wrong number of arguments or invalid property assignment.
This CODE Works now:
Sub TagTryco()
Dim listLength
listLength = Worksheets("ILS_Import").Cells(Rows.Count, "E").End(xlUp).Row - 1
Dim i As Integer
For i = 2 To listLength + 2
If Worksheets("ILS_Import").Range("E" & i) = 160248 Then
Worksheets("ILS_Import").Range("AG" & i) = "Yes"
Else
Worksheets("ILS_Import").Range("AG" & i) = "No"
End If
Next
End Sub
To know how many entries you have:
dim listLength
listlength = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row - 1 'I assumed column E, starting at row 2
You need to loop from row 2 to the row 2 + listLength, check the cell in column E, and check if it is equal to your number:
dim i as integer
for i = 2 to listLength + 2
If Range("E" & i) = 160248 Then
Range("AG" & i) = "Yes"
Else
Range("AG" & i) = "No"
End If
Next
If you wish to scan for different numbers you can adapt the code to use a value from a cell in which you enter that number, OR use an inputbox to enter the number you want to look for, or something else. This code was not tested.
If you want to use the column name you assigned instead of AG (which is safer) you can use something along the lines of:
= Range("Incorporated_160248")(i+1)
Instead, which gives the column with an offset of i. Should bring you to the right cell.
I have the below mentioned code, I am trying to load filtered cells only into the listbox but I don't know why the below mentioned code is not functional.
Legend:
PatternSearchButton is a button
PatternTextBox is a textbox by which the user enters a value Which the Sheet will filter.
WsLookup is a function which selects the sheet (completely functional)
Private Sub PatternSearchButton_Click()
Dim PatternInput As String, PatternCounter As Double, WsSelector As Worksheet
PatternInput = PatternTextBox.Value
Set WsSelector = WsLookup(GSMListType.Value)
WsSelector.Range("F:F").AutoFilter Field:=1, Criteria1:=PatternInput
PatternCounter = Application.WorksheetFunction.Subtotal(4, WsSelector.Range("F:F"))
With AvailableNumberList
.Clear
For k = 2 To PatternCounter + 1
.AddItem WsSelector.Range("A" & k).SpecialCells(xlCellTypeVisible).Value
Next k
End With
End Sub
You're are using PatternCounter as the upper limit in your For .. Next but this is being set using the MAX (e.g. 4) subfunction of SUBTOTAL. This might work on sequential numbers in an unfiltered list but it is unlikely to be accurate in a filtered list. Using the COUNT (2/102) or COUNTA (3/103) subfunction might be more appropriate.
You were using SUBTOTAL(4, ...) so I would assume that you are dealing with numbers. Use a straight count on numbers on visible cells in column F and modify the remainder of the code to resemble this.
PatternCounter = Application.WorksheetFunction.Subtotal(2, WsSelector.Range("F:F"))
With WsSelector.cells(1,1).currentregion.offset(1,0).SpecialCells(xlCellTypeVisible)
AvailableNumberList.Clear
For k = 1 To PatternCounter
AvailableNumberList.AddItem .cells(k, 1).Value
Next k
End With
The problem might stem from the fact that you add to the list box the value of a special cell that might not exist if the cell is hidden.
Try for the body of the For loop:
' ... previous code '
If Not WsSelector.Rows(k).EntireRow.Hidden Then
.AddItem WsSelector.Cells(k, 1).Value
End If
' rest of the code ... '
Also, make sure that AvailableNumberList points to the correct object in your code.
I have vba that produces a flat text file of the selected column.
The issue is that the process takes a while because usually the column letter is clicked and the whole column is highlighted including all the unused cells.
How can i get the macro to stop processing when it finds the first empty row?
Here is my code.
Sub testlist()
Open "C:\Users\gaum\Desktop\Work\NCL\testlist.lst" For Output As #1
For NR = 1 To Selection.Rows.Count
For NC = 1 To Selection.Columns.Count
ExpData = Selection.Cells(NR, NC).Value
If IsNumeric(ExpData) Then ExpData = Val(ExpData)
If IsEmpty(Selection.Cells(NR, NC)) Then ExpData = ""
If NC <> NumCols Then
If Not ExpData = "FilePath" Then Print #1, ExpData
End If
Next NC
Next NR
Close #1
End Sub
Also am i able to get the macro to produce the output if i have multiple selections i.e ctrl and left click various cells, it currently only outputs the first highlight.
Many Thanks
Since you asked 2 separate questions, I will address them both separately.
The easiest way to stop processing when you encounter a blank row is to add a check before your 2nd For..Next loop. The issue is how to check. The simplest way to check if an entire range is empty is to use the CountA worksheet function.
If WorksheetFunction.CountA(Range(NR & ":" & NR).EntireRow) = 0 Then Exit For
The above will basically use the worksheet function CountA and count the number of cells within the range that are not blank (using CountA is important here as the Count worksheet function will only count numeric cells and not non-numeric ones, whereas CountA will count anything except blanks. The other advantage you get by using the WorksheetFunction object is you can adjust the Range object as you need if you only want to check a few columns and the not the entire row by just specifying the specific Range and not using .EntireRow.
The next question is how to deal with multiple selected ranges. There is another member of the Selection class called Areas, which should given you the functionality you need. Areas is a collection that has the ranges for each individual selection range you make.
You can reference each selection range independently by using the 1-based index of the selection:
NumAreaRows = Selection.Areas(1).Rows.Count 'gets the number of rows in the first selected range
NumAreaCols = Selection.Areas(2).Columns.Count 'gets the number of columns in the second selected range
So you could put both all together into your solution:
Sub testlist()
Open "C:\Users\gaum\Desktop\Work\NCL\testlist.lst" For Output As #1
For NA = 1 To Selection.Areas.Count
For NR = 1 To Selection.Areas(NA).Rows.Count
If WorksheetFunction.CountA(Range(NR & ":" & NR).EntireRow) = 0 Then Exit For
For NC = 1 To Selection.Areas(NA).Columns.Count
ExpData = Selection.Areas(NA).Cells(NR, NC).Value
If IsNumeric(ExpData) Then ExpData = Val(ExpData)
If IsEmpty(Selection.Areas(NA).Cells(NR, NC)) Then ExpData = ""
If NC <> NumCols Then
If Not ExpData = "FilePath" Then Print #1, ExpData
End If
Next NC
Next NR
Next NA
Close #1
End Sub
The placement of the CountA function and the Exit For statement here allows you to loop through each selected range independently and it won't exit completely if you have a blank row in one of the ranges.
Given this process takes a while, you would be better off going beyond stopping at a blank cell, and removing the inefficient range loop altogether. The code below
Uses a variant array rather than range
removes the redundant two-step IF test (if ExpData is numeric it cannot also be "FilePath")
code
Sub testlist()
Dim X
Dim lngCnt As Long
X = Selection
If IsEmpty(X) Then Exit Sub
Open "C:\Users\gaum\Desktop\Work\NCL\testlist.lst" For Output As #1
For lngCnt = 1 To UBound(X)
If Len(X(lngCnt, 1)) = 0 Then Exit For
If IsNumeric(X(lngCnt, 1)) Then Print #1, Val(X(lngCnt, 1))
Next
Close #1
End Sub
I have a dated CS degree so I understand the basics of VB but I don't write macros very often and need help solving a particular condition. (...but I understand functions and object oriented programming)
Assume the following:
- Column A contains reference ID's in alphanumeric form, sorted alphabetically.
- Column B contains strings of text, or blanks.
I'm trying to write a macro that automatically removes any extra rows for each unique reference number based on the contents of the "Notes" in column B. The problem is that if column A has multiple instances of a unique ref number, I need to identify which row contains something in column B. There is one catch: it is possible that the reference number has nothing in column B and should be retained.
To explain further, in the following screenshot I would need to:
Keep the yellow highlighted rows
Delete the remaining rows
I tried to show various configurations of how the report might show the data using the brackets on the right and marked in red. Its difficult to explain what I'm trying to do so I figured a picture would show what I need more clearly.
This task is making the report very manual and time consuming.
it's pretty simple
you just go throug the rows and check whether this row needs to be deleted, an earlier row with this id needs to be deleted or nothing should happen.
in my example i mark these rows and delete them in the end.
Sub foo()
Dim rngSelection As Range
Dim startingRow As Integer
Dim endRow As Integer
Dim idColumn As Integer
Dim noteColumn As Integer
Dim idValuableRow As New Dictionary
Dim deleteRows As New Collection
Set rngSelection = Selection
startingRow = rngSelection.Row
endRow = rngSelection.Rows.Count + startingRow - 1
idColumn = rngSelection.Column
noteColumn = idColumn + 1
For i = startingRow To endRow
currentID = Cells(i, idColumn)
If idValuableRow.Exists(currentID) Then
If Trim(idValuableRow(currentID)("note")) <> "" And Trim(Cells(i, noteColumn)) = "" Then
deleteRows.Add i
ElseIf idValuableRow(currentID)("note") = "" And Trim(Cells(i, noteColumn)) <> "" Then
deleteRows.Add idValuableRow(currentID)("row")
idValuableRow(currentID)("row") = i
idValuableRow(currentID)("note") = Cells(i, noteColumn)
End If
Else
Dim arr(2) As Variant
idValuableRow.Add currentID, New Dictionary
idValuableRow(currentID).Add "row", i
idValuableRow(currentID).Add "note", Cells(i, noteColumn)
End If
Next i
deletedRows = 0
For Each element In deleteRows
If element <> "" Then
Rows(element - deletedRows & ":" & element - deletedRows).Select
Selection.Delete Shift:=xlUp
deletedRows = deletedRows + 1
End If
Next element
End Sub
it could look something like this. the only thing you need is to add Microsoft Scripting Runtime in Tools/References