简体   繁体   中英

VBA function to iterate through cells, replacing a cell with the relative column header value

I'm trying to convert a data matrix to a new standard that should fit a specific analysis software.

The initial matrix looks like this:

real char num 10 10 25 26 26 56
--------------------------------
state num     1  2  9  4  6  3
--------------------------------
name 1        0  0  1  1  0  1
name 2        1  0  0  0  0  0
name 3        0  1  1  0  0  1
name 4        0  1  0  0  1  0
name 5        1  0  0  0  0  0
name 6        0  0  1  0  1  0

I've been trying to achieve this:

real char num 10 10 25 26 26 56
--------------------------------
state num     1  2  9  4  6  3
--------------------------------
name 1        0  0  9  4  0  3
name 2        1  0  0  0  0  0
name 3        0  2  9  0  0  3
name 4        0  2  0  0  6  0
name 5        1  0  0  0  0  0
name 6        0  0  9  0  6  0

Essentially, what I'm trying to do is:
1. For every column, look in every cell for a number other than 0;
2. If this condition is achieved, replace the cell value with the relative "state" header. Meaning, for instance, if A4 <> 0, then replace it with A3 value.

The code I've used is as follows:

Sub Iterate_replace()
    Sheets("matrix").Select

    Dim r As Range, cell As Range, state As Range

    Set r = Range("C3")
    Set state = Range("C2")

    For Each cell In r
        If cell.Value <> "0" Then
            cell.Value = state.Value
        End If
    Next
End Sub

It works fine in a defined range of one single column, but I'm having trouble making it dynamic. Should I use R1C1 notation to refer to the cells in the range? Everything related that I could find never explicits how to make this iteration more flexible. Should I use nested loops? Loops are a very difficult thing for me to grasp, still, so, please be patient.

I'd appreciate if anyone could point me to the right direction. Thanks!

I am assuming that there is nothing else on each sheet than the matrix in question. In that case you should be able to make you procedure dynamic by modifying your code like the following:

Sub Iterate_replace()
    Sheets("matrix").Select
    Dim i As Integer, j As Integer
    Dim state As Range

    Set state = Range("C2")

    'Loops through each row and each column in matrix
    For i = state.Column To ActiveSheet.Cells(state.Row, Columns.Count).End(xlToLeft).Column
        For j = state.Row + 1 To ActiveSheet.Cells(Rows.Count, state.Column).End(xlUp).Row
            If Cells(j, i).Value <> 0 Then
                Cells(j, i).Value = Cells(state.Row, i).Value
            End If
        Next j
    Next i
End Sub

This will loop through each column and each row in your matrix if you have defined in what cell the most left state value is located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM