Change VBA code to use two values instead of one

RobertHamberg

New Member
Joined
Jan 11, 2018
Messages
34
Office Version
  1. 365
I got help to write a VBA-code that register values to a table based on data in one cell. I need to rewrite it so it uses to values instead of one. The first picture is how it is now, but in the second picture I have how I want it to be. I want the vba-code to use de value in A1 AND A2 when saving data to the sheet "Cars"

Skärmklipp.PNG


1Skärmklipp.PNG


VBA Code:
Sub SaveCar()
Dim vFoundDate, vFoundOmr
Dim SearchRange As Range
Dim dCar
Dim firstrow, lastrow, r, iStartrow, iStoprow
iStartrow = 3
iStoprow = 9
dCar = Sheets("Reg").Range("rSearchReg").Value
Call SortCars
Set vFoundDate = Sheets("Cars").Range("A:A").Find(dCar)
If Not vFoundDate Is Nothing Then
firstrow = Sheets("Cars").Range("A:A").Find(dCar, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row
lastrow = Sheets("Cars").Range("A:A").Find(dCar, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set SearchRange = Range(Sheets("Cars").Range("A:A").Range("B" & firstrow), Sheets("Cars").Range("B" & lastrow))
For r = iStartrow To iStoprow
If Not Sheets("Reg").Range("B" & r).Value = "" Then
Set vFoundOmr = SearchRange.Find(Sheets("Reg").Range("A" & r).Value)
If Not vFoundOmr Is Nothing Then
vFoundOmr.Offset(0, 1) = Sheets("Reg").Range("B" & r).Value
Else
lastrow = Sheets("Cars").Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Sheets("Cars").Range("A" & lastrow + 1).Value = dCar
Sheets("Cars").Range("B" & lastrow + 1).Value = Sheets("Registrering").Range("A" & r).Value
Sheets("Bilar").Range("C" & lastrow + 1).Value = Sheets("Registrering").Range("B" & r).Value
End If
End If
Next r
Else
For r = iStartrow To iStoprow
If Not Sheets("Reg").Range("B" & r).Value = "" Then
lastrow = Sheets("Cars").Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Sheets("Cars").Range("A" & lastrow + 1).Value = dCar
Sheets("Cars").Range("B" & lastrow + 1).Value = Sheets("Reg").Range("A" & r).Value
Sheets("Cars").Range("C" & lastrow + 1).Value = Sheets("Reg").Range("B" & r).Value
End If
Next r
End If
Call SortCars
End Sub
 
Change this:
VBA Code:
sh2.Range("A1").Resize(j, 4).Value = a

for this:
VBA Code:
sh2.Range("A1").Resize(j - 1, 4).Value = a
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Change this:
VBA Code:
sh2.Range("A1").Resize(j, 4).Value = a

for this:
VBA Code:
sh2.Range("A1").Resize(j - 1, 4).Value = a

Thanks worked!

The next step is to search the output sheet for rows that match the values in B1 and B2 in the source sheet and return the values if a match is found to the range B4:B10 on the source sheet. :)

Btw, where can I donate som money to your work for all the help you are giving?
 
Upvote 0
The next step is to search the output sheet for rows that match the values in B1 and B2 in the source sheet and return the values if a match is found to the range B4:B10 on the source sheet.
I didn't understand that part, is it something new or is it something that was missing from my macro?


Btw, where can I donate som money to your work for all the help you are giving?
Thanks for the intention. But don't worry, no contribution is necessary, we do it for the pleasure of helping.
 
Upvote 0
I didn't understand that part, is it something new or is it something that was missing from my macro?

Its new. :)

Mätarställningar 2020.xlsm
AB
1Reg.nummerBMW123
2Date2020-08-28
3
4Modell
5Märke
6Årsmodell
7Kontaktperson
8Kostnadsställe
9Kostnadsställe_text
10Drivmedel
Registrering


I want the macro to search the value in the above image in B1 and B2 and search the table below and if it find matching values in column A and B then return the values from column D and copy them to range B4:B10 on the above image.

Mätarställningar 2020.xlsm
ABCD
1DateReg.nummerTypeValue
22020-08-28BMW123Modell1
32020-08-28BMW123Märke2
42020-08-28BMW123Årsmodell3
52020-08-28BMW123Kontaktperson4
62020-08-28BMW123Kostnadsställe5
72020-08-28BMW123Kostnadsställe_text6
82020-08-28BMW123Drivmedel7
Bilar
 
Upvote 0
Put the following code in the events of your "Registrering" sheet

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, dic As Object
  Dim i As Long, reg As String
  
  If Not Intersect(Target, Range("B1:B2")) Is Nothing Then
    If Target.CountLarge > 1 Then Exit Sub
    Set sh1 = Sheets("Registrering")                      'source sheet
    Set sh2 = Sheets("Bilar")                             'output sheet
    Set dic = CreateObject("Scripting.Dictionary")
    dic.comparemode = vbTextCompare
    a = sh2.Range("A2", sh2.Range("D" & Rows.Count).End(3)).Value2                 'data in bilar. + 7: because they are 7 types
    For i = 1 To UBound(a, 1)
      dic(a(i, 1) & "|" & a(i, 2) & "|" & a(i, 3)) = a(i, 4)   'bilar: date | numm | type
    Next
    
    For i = 4 To 10
      reg = sh1.Range("B2").Value2 & "|" & sh1.Range("B1").Value2 & "|" & sh1.Range("A" & i).Value2 'date | numm | type
      Range("B" & i).Value = dic(reg)
    Next
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0
Put the following code in the events of your "Registrering" sheet

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, dic As Object
  Dim i As Long, reg As String
 
  If Not Intersect(Target, Range("B1:B2")) Is Nothing Then
    If Target.CountLarge > 1 Then Exit Sub
    Set sh1 = Sheets("Registrering")                      'source sheet
    Set sh2 = Sheets("Bilar")                             'output sheet
    Set dic = CreateObject("Scripting.Dictionary")
    dic.comparemode = vbTextCompare
    a = sh2.Range("A2", sh2.Range("D" & Rows.Count).End(3)).Value2                 'data in bilar. + 7: because they are 7 types
    For i = 1 To UBound(a, 1)
      dic(a(i, 1) & "|" & a(i, 2) & "|" & a(i, 3)) = a(i, 4)   'bilar: date | numm | type
    Next
   
    For i = 4 To 10
      reg = sh1.Range("B2").Value2 & "|" & sh1.Range("B1").Value2 & "|" & sh1.Range("A" & i).Value2 'date | numm | type
      Range("B" & i).Value = dic(reg)
    Next
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.

@DanteAmor
New problem :)

Sammanställning körjournal 2020.xlsm
AB
1Reg.nummer
2Datum
3
4Mätarställning Start
5Mätarställning ****
6Tankat antal liter
7Kostnad
8Förare
Registrering
Cells with Data Validation
CellAllowCriteria
B1List=Data!$G$2:$G$150


Sammanställning körjournal 2020.xlsm
ABCD
1DatumReg.nummerTypeValue
22020-12-23BDH42ZMätarställning Start2796
32020-12-23BDH42ZMätarställning ****7833
42020-12-23BDH42ZTankat antal liter229
52020-12-23BDH42ZKostnad3224
62020-12-23BDH42ZFörareAlla
Mätarställning


The code search for value in cells "B1" and "B2" and if a match is found on sheet "Mätarställning" then it returns the values to cells B4:B8 on sheet "Registrering". What I need is for it to also return a value if there isn't a match.

An example: I write "BDH42Z" in cell "B1" and date "2020-12-24" in cell "B2". Currently no value whould be returned as no registration exists in sheet "Mätarställning" with that date. What I want is for the code then to retrive the value from column "D" on sheet "Mätarställning" from the row with type (column C) "Mätarställning ****" from the date thats closest to the date that has a registration, in this case 2020-12-24 but could also be a date futher back in time.
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top