Store a specific column value until next value is identified

urskrishna18

New Member
Joined
Jan 28, 2022
Messages
9
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
I have very basic knowledge in VBA, I have one requirement where the data for a specific set will be available in multiple rows and it will be like Header (ex. from A to D) and lines (ex. from E:J). Header will not be there for all the line rows, so when validating the line data with Header data, I need to validate the value in for ex. col "D2" with all the lines and when the new header is available, follow the same pattern again.

Below is the sample data format, in which col "A:D" represents Header data and from col "E:J" represents line data. Through out the line validation with Header, I want col "D" value to be captured through out that iteration.

HjCvK.png


Ex. When the "ABC" value is iterated, "PO1" should be available for validation for rows "E2:J4". After this iteration and when the new value on Col A, i.e "DEF" is encountered then the value in Col "D" to be changed like "PO2" from the next 2 rows of the Header.

As I have very basic knowledge in VB, not sure how I can achieve this. Can anyone help me here please?

I tried to compare the first value with the next available value in Col A, during this iteration tried storing the Col "D" value to a global variable but it is not giving me expected results

Sample code that i tried:

VBA Code:
Public v As Integer

Sub inv()

Dim i As Integer, j As Integer, temp As Integer, rng As Range
Dim lastRow As Integer, lastRowSheet2 As Integer
Dim sheet1 As Worksheet, Sheet2 As Worksheet

Set sheet1 = Sheets("Data")
Set Sheet2 = Sheets("res")
lrow = sheet1.Range("A1").SpecialCells(xlCellTypeLastCell).Row


For i = 2 To lrow
    
    If sheet1.Range("A" & i).Value <> "" Then
        invv = sheet1.Range("A" & i).Value
        v = i

    End If
    If sheet1.Range("A" & i + 1).Value <> "" Then
        ninv = sheet1.Range("A" & i + 1).Value

    End If

    If invv <> ninv And sheet1.Range("A" & i + 1).Value <> "" Then
    
        Sheet2.Range("A" & i).Value = sheet1.Range("D" & v).Value
        MsgBox "Alert -Entry in row is not equal to Previous Cell !!"
        
        'Exit Sub
    End If

Next i



End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
VBA Code:
Sub myFunction()
  Dim lRow As Long, iRow As Long, eRow As Long
  lRow = Cells(Rows.Count, 5).End(xlUp).Row
  
  For i = 2 To lRow
    If Cells(i, 1).Value <> "" Then
      iRow = Range("A" & i).End(xlDown).Row - 1
      If iRow > lRow Then iRow = lRow
      For j = i To iRow
        'Do what ever you want with Cells(i, 4).Value :)
      Next
    End If
  Next
End Sub
For example, let's fill all rows with Column D value:
VBA Code:
Sub myFunction()
  Dim lRow As Long, iRow As Long, eRow As Long
  lRow = Cells(Rows.Count, 5).End(xlUp).Row
  
  For i = 2 To lRow
    If Cells(i, 1).Value <> "" Then
      iRow = Range("A" & i).End(xlDown).Row - 1
      If iRow > lRow Then iRow = lRow
      For j = i To iRow
        Cells(j, 4).Value = Cells(i, 4).Value
      Next
    End If
  Next
End Sub
 
Last edited by a moderator:
Upvote 0
Solution
VBA Code:
Sub myFunction()
  Dim lRow As Long, iRow As Long, eRow As Long
  lRow = Cells(Rows.Count, 5).End(xlUp).Row
 
  For i = 2 To lRow
    If Cells(i, 1).Value <> "" Then
      iRow = Range("A" & i).End(xlDown).Row - 1
      If iRow > lRow Then iRow = lRow
      For j = i To iRow
        'Do what ever you want with Cells(i, 4).Value :)
      Next
    End If
  Next
End Sub
For example, let's fill all rows with Column D value:
VBA Code:
Sub myFunction()
  Dim lRow As Long, iRow As Long, eRow As Long
  lRow = Cells(Rows.Count, 5).End(xlUp).Row
 
  For i = 2 To lRow
    If Cells(i, 1).Value <> "" Then
      iRow = Range("A" & i).End(xlDown).Row - 1
      If iRow > lRow Then iRow = lRow
      For j = i To iRow
        Cells(j, 4).Value = Cells(i, 4).Value
      Next
    End If
  Next
End Sub
@Flashbond, Thank you for the quick response. I think this gave me the result that i am looking for and an idea on how to take care of the other logic, as this is a piece of code in another script. Thanks for your help :)
 
Upvote 0

Forum statistics

Threads
1,215,799
Messages
6,126,975
Members
449,351
Latest member
Sylvine

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