finding cells with value in table and copy Row & column Header & cell Value in new sheet

Ghadir

New Member
Joined
Nov 24, 2018
Messages
3
Hello
I'm totally new in excel and trying to learn macro, I searched the forum and didn't find the correct macro but now I need a macro to search a table for cells with value, then copy row and column headers and the cell value in a row in new sheet. as example:
city1234
Axz
B
Cy
Dw

<tbody>
</tbody>

output:
A2x
A4z
C1y
D4w

<tbody>
</tbody>

thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this for results on sheet2, starting "A2".
Code:
[COLOR="Navy"]Sub[/COLOR] MG24Nov10
[COLOR="Navy"]Dim[/COLOR] ray [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
 ray = ActiveSheet.Cells(1).CurrentRegion
   ReDim nray(1 To UBound(ray, 1), 1 To 3)

[COLOR="Navy"]For[/COLOR] n = 2 To UBound(ray, 1)
  [COLOR="Navy"]For[/COLOR] Ac = 2 To UBound(ray, 2)
    [COLOR="Navy"]If[/COLOR] ray(n, Ac) <> "" [COLOR="Navy"]Then[/COLOR]
        c = c + 1
        nray(c, 1) = ray(n, 1)
        nray(c, 2) = ray(1, Ac)
        nray(c, 3) = ray(n, Ac)
    [COLOR="Navy"]End[/COLOR] If
   [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A2").Resize(c, 3)
    .Value = nray
    .Borders.Weight = 2
    .Columns.AutoFit
    .HorizontalAlignment = xlCenter
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
thanks Mick, but this just work with 2 columns. once No. of coluns is more than 2 it stop working with error "subscript out of range" for line 9 [nray(c, 1)= ray(n, 1)}
 
Upvote 0
thanks Mick, but this just work with 2 columns. once No. of coluns is more than 2 it stop working with error "subscript out of range" for line 9 [nray(c, 1)= ray(n, 1)}


i found the source of error.it caused by dimension of nray, the problem is the number of defined rows for nray by using Ubound function which the max number of rows will be equal to number of rows of ray not the amount of cells in the range. i would be appreciated if you let me know the best way to define count of cells as array dimension.
 
Upvote 0
Try changing the array "nRay" dimensioning to the following:-
Code:
ReDim nray(1 To UBound(ray, 1[COLOR="#FF0000"][B])*uboound(ray,2), [/B][/COLOR]1 To 3)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,753
Messages
6,126,677
Members
449,327
Latest member
John4520

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