I have attempted using different VBA subs to pull data from my [MasterRenamed] datasheet into my [P&L] sheet using Category, and date ranges.
While I got the first to work, I was unable to figure out how to make it dynamic (i.e. I could only do one item at a time). I completely failed with the second for whatever reason.
I would upload the workbook but maybe because I am new to the forum I am not allowed to upload docs yet(?). I am not set on any particular code format as long as it works. I would certainly appreciate some guidance.
Thank you both for your responses. I was able to figure it out, and believe i've come up with a pretty good solution comparatively to what i was able to find online.
this macro uses a search list in excel (col k) to search through the data set (bank and credit card statements) and returns combined totals (like Excel's native consolidate function) down the column search list.
Sub Search_and_Consolidate_Sums()
Dim ws1 As Worksheet
Set ws1 = Sheets("MasterRenamedx")
ws1.Select
Dim lrow1 As Long, lrow2 As Long
' first lrow is used in the lookup data range, the second lrow is for the lookup list.
lrow1 = Range("B" & Rows.Count).End(xlUp).Row
lrow2 = Range("K" & Rows.Count).End(xlUp).Row
Dim rng1 As Range ' lookup list
Set rng1 = Range("k1")
stDate = Range("J1") ' start & end dates
EndDate = Range("j2")
Dim Dates As Range, Categories As Range, Debits As Range, Credits As Range ' Data range
Set Dates = Sheets("MasterRenamedx").Range("B2:B" & lrow1)
Set Categories = Sheets("MasterRenamedx").Range("C2:C" & lrow1)
Set Debits = Sheets("MasterRenamedx").Range("E2:E" & lrow1)
For i = 2 To lrow2
mytotals = Application.WorksheetFunction.SumIfs(Debits, Dates, _
">=" & stDate, Dates, "<=" & EndDate, Categories, Cells(i, 11))
Cells(i, 12) = mytotals
Next i
End Sub
Related
I have exported CSV files from a Development SQL Server and another from Production.
The table (in the database) has two columns
UserID
DocumentID
both of these should be unique values.
I want to be able to verify that those two combinations (together) match the other environment.
So far I imported both CSV files in separate worksheets in Excel.
After this, I am not sure what I should do to compare these columns?
I did a little google-ing and there are so many different types of answers but not sure how to do it.
Conditional Formatting only works if I select a single column. I need to get the combination of both columns.
A quick and mildly dirty VBA-approach. I assumed your workbook consists of two worksheets, each containing two columns with headers.
Option Explicit
Sub SoftwareIsFun()
Dim wks1 As Worksheet
Dim wks2 As Worksheet
Dim dicObj As Object
Dim lastRow1 As Long
Dim lastRow2 As Long
Dim i As Long
Set dicObj = CreateObject("Scripting.Dictionary")
Set wks1 = ThisWorkbook.Worksheets(1)
Set wks2 = ThisWorkbook.Worksheets(2)
With wks1
lastRow1 = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow1
If Not dicObj.Exists(.Range("A" & i).Value) Then
dicObj.Add .Range("A" & i).Value, .Range("B" & i).Value
Else
.Range("C" & i).Value = "UserID already exists"
End If
Next i
End With
With wks2
lastRow2 = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow2
If dicObj.Exists(.Range("A" & i).Value) Then
If .Range("B" & i).Value = dicObj.Item(.Range("A" & i).Value) Then
.Rows(i).Interior.Color = vbGreen
Else
.Rows(i).Interior.Color = vbRed
End If
Else
.Rows(i).Interior.Color = vbRed
End If
Next i
End With
End Sub
What you are describing is something I do daily for my job:
Step 1
Create a 3rd column in both worksheets called "Key" where you'll concatenate the values for Column's A & B as follows:=A2&B2.
Now autofill your rows in column C with the previous formula you've written.
Step 2
Remove duplicates found in this column you've created, this will effectively preserve pairs and prevent information loss when removing duplicate values. (Data Tab -> Remove Duplicates -> Select column C as the criteria to remove them).
Step 3
Make a Vlookup in a 4th column in your first worksheet, the function takes 4 parameters: =vlookup(C2, <4th column of the other worksheet (select entire range from row 2 to end)>, 1, 0) and autofill your rows with the formula.
If you aren't yet familiar with vlookup yet I strongly advice you watch a brief tutorial on its usage, it is an essential tool to compare data.
Any value that matches will be displayed, whereas an #N/D error will print for those which don't match between the 2 tables.
I’m trying to copy the current row from the DataGridView into Excel but can’t seem to find the correct syntax for it to work. Hopefully someone can help me out with the below code.
Thanks in advance.
Public Sub CopyRowToExcel()
Dim xlApp As Excel.Application
Dim WB As Excel.Workbook
Dim WS As Excel.Worksheet
Dim LR As Integer
Dim RowNum As Integer
xlApp = GetObject(, Constants.ExcelApp)
WB = xlApp.Workbooks("Products")
WS = WB.Sheets("DataSheet")
LR = WS.Range("A" & WS.Rows.Count).End(Excel.XlDirection.xlUp).Row + 1
RowNum = DataGridView1.CurrentRow.Index
WS.Range("A" & LR & ":L" & LR).Value = DataGridView1.Rows(RowNum).Cells(1 To 12).Value
End Sub
You do not say if the grid has a data source. If you do not have a data source for the grid, then I suggest you use one. It will make things easier in numerous ways, and this is an example.
Assuming the grid has a DataTable as a DataSource, then you should be able to write a whole row to the Excel file similar to what you appear to be asking with…
DataGridView1.Rows(RowNum).Cells(1 To 12).Value
When you set a ranges value in Excel like…
WS.Range("A" & LR & ":L" & LR).Value =
… will work as long as the right-hand side of the “=” is an array of the same dimensions. In this case where the range is a “single” row, then the array needs to be a ONE (1) dimensional array. Then, if the range is larger than the array, it will fill the missing array part(s) with something like… “#N/A” … And if the range is less than the size of the array, then it will ignore the extra values in the array.
If the range dimension is not the same as the array dimension… you will get an error.
Therefore if the grid used a DataTable and you wanted to append the selected row to the end of an existing worksheet, then you should be able to do something like…
Dim excelRange As String = "A" & LR & ":L" & LR
Dim drv As DataRowView = DataGridView1.SelectedRows(0).DataBoundItem
xlWorksheet.Range(excelRange, Type.Missing).Value2 = drv.Row.ItemArray.ToArray()
In my small tests, this worked as described above.
Been trying all kinds of things to get this to work and I cant figure it out. Using a table, I am trying to use VBA to take the value of the last cell in the last column (column H in my case), and if it is > 0 then copy the last cell in column C (which is not in the table range, its just a regular cell) to the next cell below. Any help would be greatly appreciated. This is the last missing piece of this puzzle to finish this workbook!I dont have enough reputation to post an image but more than willing to email the image or the workbook to whomever is willing to help.
Sub Test()
Dim ws as Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<-- Update
Dim lrH as Long, lac as Long
lrH = ws.Range("H" & ws.Rows.Count).End(xlUp).Row
If ws.Range("H" & lr) > 0 Then
lrC = ws.Range("C" & ws.Rows.Count).End(xlUp).Row
ws.Range("H" & lrH + 1).Value = ws.Range("C" & lrC)
End If
Rather simple question that I seem to be struggling with.
I have 2 columns. column1 has dates in it where each cell is the day after the previous. column 2 occasionally has data in it. i need to apply an auto filter show all dates except the 15th and 26th of the month for column 1 and all blank cells in column2. I can do the column2 filter with the code
ws1.Range(Cells(4, 1), Cells(x, 2)).AutoFilter field:=2, Criteria1:="="
but i cant figure out how to filter column 1.
any help would be appreciated.
Thank you.
i need to apply an auto filter show all dates except the 15th and 26th of the month for column 1
You can achieve what you want using an array of relevant dates.
Logic: Create an array and then use Criteria2 paramenter to filter. For example if you record a macro you will get something like this Criteria2:=Array(2, "1/1/2020", 2, "1/4/2020", 2, "1/6/2020").... First dimension of array is the time period group. 2 is for Days.
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim rng As Range
Dim lRow As Long, i As Long
Dim DateArray As Variant
Dim tmpDateString As String: tmpDateString = "2"
'~~> Set your worksheet here
Set ws = Sheet2
With ws
'~~> Remove any autofilter
.AutoFilterMode = False
'~~> Find the last row
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Check if there is any data
If lRow > 1 Then
'~~> Construct your range
Set rng = .Range("A1:A" & lRow)
'~~> Create a string with relevant dates
For i = 2 To lRow
Select Case Day(.Range("A" & i).Value)
Case 15, 16
Case Else
tmpDateString = tmpDateString & "," & .Range("A" & i).Value & ",2"
End Select
Next i
End If
'~~> Split and store in an array
tmpDateString = Mid(tmpDateString, 1, Len(tmpDateString) - 2)
DateArray = Split(tmpDateString, ",")
'~~> Autofilter using an array
rng.AutoFilter Field:=1, Criteria2:=DateArray, Operator:=xlFilterValues
End With
End Sub
In Action
If i could get it to show only 15th and 26th that would work as well as im filtering to delete unused dates. – matthew wilcox 1 hour ago
Swap the Case code in Select Case. Give it a try ;)
Starting from Sheet "DATA" range B4:Hx, where x is my last row taking by a row count. I need to copy this range and paste it as values on sheet "bat" starting at A1.
Going forward I need to offset columns in 6. So my second copy will be I4:Ox and so one copying appending into bat sheet.
I know where I must stop and I'm informing it using the Funds value.
The first error I'm having is when I try set Column2 = Range("H" & bottomD) value that is giving me "overflow".
And sure I don't know yet if my For loop would work.
Sub Copy_bat()
Dim bottomD As Integer
Dim Column1 As Integer
Dim Column2 As Integer
Dim i As Integer
Dim Funds As Integer
Funds = Sheets("bat").Range("u3").Value
Sheets("DATA").Activate
bottomD = Range("A" & Rows.Count).End(xlUp).Row
Column1 = Range("B4")
Column2 = Range("H" & bottomD)
For i = 1 To Funds
Range(Column1 & ":" & Column2).Copy
Sheets("Data").Cells(Rows.Count, "A").End(xlUp)(2).PasteSpecial Paste:=xlPasteValues, SkipBlanks:=True, Transpose:=False
Column1 = Colum1.Range.Offset(ColumnOffset:=6)
Column2 = Colum2.Range.Offset(ColumnOffset:=6)
Next i
End Sub
Always use Option Explicit at the beginning of every module to prevent from typos. Always! You had typos at the bottom - Colum1 and Colum2.
Avoid Activate and Select (you had Sheets("DATA").Activate) - better performance, smaller error chance. Instead, you should always explicitly tell VBA which sheet you are referring to.
While pasting values you can simply do something like Range2.value = Range1.value. No need to .Copy and then .Paste.
I did my best to understand what you need. From my understanding you did not use Range data type, while you needed that. This caused you errors.
Option Explicit
Sub Copy_bat()
Dim bottomD As Integer
Dim i As Integer
Dim Funds As Integer
Dim rngArea As Range
Funds = Sheets("bat").Range("u3").Value
With Sheets("Data")
bottomD = .Range("A" & .Rows.Count).End(xlUp).Row
Set rngArea = Range(.Range("B4"), .Range("H" & bottomD))
End With
For i = 1 To Funds
Sheets("bat").Cells(Rows.Count, "A").End(xlUp)(2).Resize(rngArea.Rows.Count, rngArea.Columns.Count).Value = _
rngArea.Value
Set rngArea = rngArea.Offset(, 7)
Next
End Sub
I made one rngArea variable of type Range instead of 2 variables (Column1 and Column2). This code takes info from "Data" sheet and puts that to "bat" sheet. Then offsets to right by 7(!) columns in "Data" sheet and puts data in "bat" sheet below the data that was put previously.