|
本帖最后由 leepoison 于 2024-10-18 14:36 编辑
VBA,
1.先判断sheet2是否为空。
如果为空,将sheet1有公式的所有格内的公式不带等号,剪切到sheet2对应的位置,并隐藏sheet2
如果不为空,将sheet2有公式的所有格内的公式带等号,剪切到sheet1对应的位置,并隐藏sheet2
- Sub MoveFormulasWithCheck()
- Dim ws1 As Worksheet
- Dim ws2 As Worksheet
- Set ws1 = ThisWorkbook.Sheets("Sheet1")
- Set ws2 = ThisWorkbook.Sheets("Sheet2")
-
- Dim isSheet2Empty As Boolean
- isSheet2Empty = True
-
- ' 判断 Sheet2 是否为空
- For Each cell In ws2.UsedRange
- If Not IsEmpty(cell.Value) Then
- isSheet2Empty = False
- Exit For
- End If
- Next cell
-
- If isSheet2Empty Then
- MoveFormulasFromSheet1ToSheet2 ws1, ws2
- ws2.Visible = xlSheetHidden
- Else
- MoveFormulasFromSheet2ToSheet1 ws1, ws2
- ws2.Visible = xlSheetHidden
- End If
- End Sub
- Sub MoveFormulasFromSheet1ToSheet2(ws1 As Worksheet, ws2 As Worksheet)
- Dim cell As Range
- For Each cell In ws1.UsedRange
- If cell.HasFormula Then
- ws2.Cells(cell.Row, cell.Column).Value = Mid(cell.Formula, 2)
- cell.ClearContents
- End If
- Next cell
- End Sub
- Sub MoveFormulasFromSheet2ToSheet1(ws1 As Worksheet, ws2 As Worksheet)
- Dim cell As Range
- For Each cell In ws2.UsedRange
- If Not IsEmpty(cell.Value) Then
- ws1.Cells(cell.Row, cell.Column).Formula = "=" & cell.Value
- cell.ClearContents
- End If
- Next cell
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|