我正在尝试选择动态表格 - 我可以找出脚本的复制/粘贴部分,但我无法弄清楚如何最初选择此表格。
该表在行数和列数方面都是动态的。这是因为这个单独的工作簿需要由不同的业务部门使用,调用不同的SQL服务器表。因此,用户将他们的输入放在Sheet1中,刷新连接,并在Sheet2中返回一个表。
以下是片段,包括工作和损坏的部分:
'Variable designations
Dim rowcount As String
Dim columncount As String
Dim sheetref1 As String
Dim sheetref2 As String
Dim rangeselect1 As String
Dim rangeselect2 As String
rowcount = Cells(Rows.Count, 1).End(xlUp).Row
columncount = Cells(1, Columns.Count).End(xlToLeft).Column
sheetref1 = "Sheet1"
sheetref2 = "Sheet2"
rangeselect1 = "A2:A" & rowcount
rangeselect2 = "A1:" & columncount & rowcount '<--BROKEN
'Copy column with populated rows
Sheets(sheetref1).Range(rangeselect1).Copy '<--WORKING
'Copy table with populated rows and columns
Sheets(sheetref2).Range(rangeselect2).Copy '<--BROKEN
所以这里rangeselect2 = "A1:" & columncount & rowcount
我试图返回类似A1:Z10或A1:F3000的东西 - 动态范围(顺便说一句,A1是静态的)。我的尝试是让columncount返回&#34; Z&#34;或&#34; F&#34;或者最后一个列字母,而rowcount(希望)正确返回最后一个行号。
希望这是有道理的。我很乐意回答任何进一步的问题,我非常感谢任何建议/帮助。
答案 0 :(得分:1)
这将找到并选择从A1到最后一个单元格的所有单元格,其中包含代码所在工作簿的Sheet1上的数据:
Sub SelectDynamicTable()
Dim rFinalRange As Range
Dim lLastRow As Long
Dim lLastCol As Long
With ThisWorkbook.Worksheets("Sheet1")
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
If lLastCol = 0 Then lLastCol = 1
If lLastRow = 0 Then lLastRow = 1
Set rFinalRange = .Range(.Cells(1, 1), .Cells(lLastRow, lLastCol))
End With
rFinalRange.Select
End Sub
使用单元格而不是范围 - 单元格可以接受行号和列号而不是字母标记。
当给出工作表参考和可选列时,此函数将返回对最后一个单元格的引用。
Public Function LastCell(wrkSht As Worksheet, Optional Col As Long = 0) As Range
Dim lLastCol As Long, lLastRow As Long
On Error Resume Next
With wrkSht
If Col = 0 Then
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
Else
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Columns(Col).Find("*", , , , xlByColumns, xlPrevious).Row
End If
If lLastCol = 0 Then lLastCol = 1
If lLastRow = 0 Then lLastRow = 1
Set LastCell = wrkSht.Cells(lLastRow, lLastCol)
End With
On Error GoTo 0
End Function
此测试程序将显示activeworkbook第2列中最后一个单元格的地址:
Public Sub TestLastCell()
MsgBox LastCell(ActiveWorkbook.Worksheets("Sheet2"), 2).Address
End Sub
如果您想在代码中找到第一个单元格,请使用 xlNext 而不是 xlPrevious 。
答案 1 :(得分:1)
您的def construct_queryset(self, user=None):
queryset = Widget.objects.all()
if self.order_by_field == 'annotate_review':
result_qs = queryset.annotate(
review_count=Count('review')
).order_by('-review_count')
return result_qs
无法正常工作,因为class Widget (models.Model):
name = models.CharField(max_length=255)
description = models.CharField(max_length=255)
class Review (models.Model):
widget = models.ForeignKey(Widget)
name = models.CharField(max_length=255)
rating = models.IntegerField(
validators=[MinValueValidator(1), MaxValueValidator(5)]
)
和rangeselect2 = "A1:" & columncount & rowcount
将作为指示行和列的数字返回。如果您想将columncount
和rowcount
翻译成A1地址(例如第2列和第5行是columncount
),那么您可以使用rowcount
,它返回绝对值该坐标的参考。
B5
应该适用于您在此尝试做的事情。