简体   繁体   English

您可以使用VBA将非结构化引用转换为结构化引用吗? (Excel 2010)

[英]Can You Turn Unstructured References to Structured Using VBA? (Excel 2010)

I have an excel table (an actual table ie Insert > Table) that uses structured references in all of the formulas. 我有一个excel表(实际表,即Insert> Table),该表在所有公式中都使用结构化引用。 Example: =[@[Predicted Total 2015 Days]]-COUNTA(All.Departments[@[1]:[40]]) 示例: =[@[Predicted Total 2015 Days]]-COUNTA(All.Departments[@[1]:[40]])

The problem I'm having is I want to sort several of the columns from Left to Right. 我遇到的问题是我想从左到右对几个列进行排序。 Excel doesn't let you do this with tables, only ranges (at least from what I've found on the subject) So it seems that my only option is to unlist the table (convert it to a range) and then convert it back. Excel不允许您仅使用表格来执行此操作,而只能使用范围(至少从我在主题中找到的内容)进行操作,因此看来,我唯一的选择是取消列出表格(将其转换为范围),然后将其转换回。 However after converting it back to a table my formulas lose their structured references Example : =Sheet2!$V2-COUNTA(Sheet2!$X2:$BK2) 但是,将其转换回表后,我的公式会丢失其结构化引用示例: =Sheet2!$V2-COUNTA(Sheet2!$X2:$BK2)

Is there any way to convert these formulas back to structured? 有什么办法可以将这些公式转换回结构化的? Or even better yet, a way to sort left to right without having to convert my table back and forth? 甚至更好,一种无需左右转换表就可以从左到右排序的方法?

I was able to sort columns using the structured references in Excel 2010 with no issues. 我能够使用Excel 2010中的结构化引用对列进行排序,没有任何问题。 Formulas within the table using structured references to other columns and data were sorted. 使用对其他列和数据的结构化引用对表中的公式进行了排序。 No converting formulas back and forth. 无需来回转换公式。

Option Explicit

Sub SortTable()
    Dim ws As Worksheet
    Dim tbl As ListObject

    Set ws = ActiveSheet
    Set tbl = ws.ListObjects("MyTable")

    With tbl.Sort
        .SortFields.Clear
        .SortFields.Add Key:=ws.Range(tbl.Name & "[PredictedTotal]"), _
                        SortOn:=xlSortOnValues, Order:=xlDescending, _
                        DataOption:=xlSortNormal
        .SortFields.Add Key:=ws.Range(tbl.Name & "[AllDepartments]"), _
                        SortOn:=xlSortOnValues, Order:=xlAscending, _
                        DataOption:=xlSortNormal
        .header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

I figured out a way around converting my table to a range. 我想出了一种将表转换为范围的方法。 In case anyone was curious I decided to post my solution. 万一有人好奇,我决定发布我的解决方案。

It's now set up to copy the entire table, Paste it as transposed values into a destination worksheet, where it then does a loop sorting each column's dates from oldest to newest, and then it takes those sorted dates and pastes them as transposed values back into the original table. 现在,它已设置为复制整个表,将其作为转置后的值粘贴到目标工作表中,然后在其中进行循环,将每列的日期从最早到最新进行排序,然后获取这些排序日期并将其作为转置后的值粘贴回原始表。

Yay to figuring out my own solution! 是的,想出我自己的解决方案! =D = d

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Application.ScreenUpdating = False

Dim SourceWS As Worksheet
Dim DestWS As Worksheet
Dim TblRange As Range
Dim DataRange As Range
Dim LastRow As Long

Set SourceWS = Worksheets("For HR Use ONLY")
Set DestWS = Worksheets("Transposed Table")

DestWS.Visible = xlSheetVisible
DestWS.Cells.ClearContents

SourceWS.Activate

LastRow = SourceWS.Range("A1").CurrentRegion.Rows.Count
Set TblRange = SourceWS.Range("A1:BM" & LastRow)

TblRange.Copy
DestWS.Range("A1").PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, True, True
Application.CutCopyMode = False

Set DataRange = SourceWS.Range("O2:BB" & LastRow)
DataRange.ClearContents

DestWS.Activate
Dim x As Long, LastCol As Long
LastCol = SourceWS.Range("A1").CurrentRegion.Rows.Count
For x = 2 To LastCol
Range(Cells(15, x), Cells(54, x)).Sort Key1:=Cells(15, x), Order1:=xlAscending, Header:=xlGuess
Next x

DestWS.Range(Cells(15, 2), Cells(54, LastCol)).Copy

SourceWS.Range("O2").PasteSpecial xlPasteValues, lPasteSpecialOperationNone, True, True
Application.CutCopyMode = False

DestWS.Visible = xlSheetHidden

SourceWS.Activate
SourceWS.Range("A1").Select

Set SourceWS = Nothing
Set DestWS = Nothing
Set TblRange = Nothing
Set DataRange = Nothing

Application.ScreenUpdating = True

End Sub

I was confronted by the same problem. 我遇到了同样的问题。 Use the following recipe: 使用以下配方:

  1. CTRL+A // Select entire table. CTRL + A //选择整个表。
  2. CTRL+H // Replace all equal signs with | CTRL + H //将所有等号替换为| or character of your choice. 或您选择的字符。
  3. ALT+J+G // Convert to range. ALT + J + G //转换为范围。
  4. ALT+H+S+U+O+L // Special Sort from left-to-right. ALT + H + S + U + O + L //特殊从左到右排序。 Choose sorting row. 选择排序行。
  5. CTRL+T // Convert back to Table. CTRL + T //转换回表。 Remember to rename it! 记住要重命名!
  6. CTRL+H // Put back all your equal signs. CTRL + H //放回所有等号。

You're done. 你完成了。 Caveat: if you have any array formulas, you'll have to recommit them with CTRL+SHIFT+ENTER 注意:如果有任何数组公式,则必须使用CTRL + SHIFT + ENTER重新提交

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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